pycharm提示This inspection detects instance attribute definition outside __init__ method
示例代码:
class MiNiCarStore(CarStore):
def createCar(self, typeName):
self.carFactory = CarFactory() # 会出现下划线提示This inspection detects instance attribute definition outside __init__ method
return self.carFactory.createCar(typeName)
原因是:根据 SRP(Single Pesponsibility Principle, SRP) 原则,这个类本来就会承担某个界面逻辑,那么它就不应该再承担“初始化”的职责,初始化的工作应该在另一个单独的类中完成,这样能让代码更可测(也就是更好写单元测试)
可改写如下:
class MiNiCarStore(CarStore):
def __init__(self):
self.carFactory = None
def createCar(self, typeName):
self.carFactory = CarFactory()
return self.carFactory.createCar(typeName)
也可以在settings -> editor -> inspections -> python 取消勾选提示
扩展阅读
单一职责原则(Single Pesponsibility Principle, SRP)
单一职责有两个含义: 一个是避免相同的职责分散到不同的类中, 别一个是避免一个类承担太多职责
为什么要遵守SRP呢?
- 可以减少类之间的耦合
如果减少类之间的耦合,当需求变化时,只修改一个类,从而也就隔离了变化;如果一个类有多个不同职责,它们耦合在一起,当一个职责发生变化时,可能会影响到其他职责。 - 提高类的复用性
修改电脑比修理电视机简单多了。主要原因就在于电视机各个部件之间的耦合性太高,而电脑则不同,电脑的内存、硬盘、声卡、网卡、键盘灯等部件都可以很容易地单独拆卸和组装。某个部件坏了,换上新的即可。上面的例子就体现了单一职责的优势。由于使用了单一职责,使得‘组件’可以方便地‘拆卸’和‘组装’。
不遵守SRP会影响对类的复用性。当只需要用该类的某一个职责时,由于它和其他的职责耦合在一起,也就很难分离出。
遵守SRP在实际代码开发中有没有什么应用?有的。以数据持久层为例,所谓的数据持久层主要指的是数据库操作,当然,还包括缓存管理等。这时就需要数据持久层支持多种数据库。应该怎么做?定义多个数据库操作类?想法已经很接近了,再进一步,就是使用工厂模式。
工厂模式(Faction)允许你在代码执行时实例化对象。它之所以被称为工厂模式是因为它负责‘生产对象’。以数据库为例,工厂需要的就是根据不同的参数,生成不同的实例化对象。最简单的工厂就是根据传入的类型名实例化对象,如传入MySQL,就调用MySQL类并实例化,如果是SQLite,则调用 SQLite的类并实例化,甚至还可以处理TXT、Execl等‘类数据库’。
pycharm提示This inspection detects instance attribute definition outside __init__ method的更多相关文章
- pycharm提示This inspection detects any methods which may safely be made static.
示例代码: class Car(object): # 未定义任何类属性 def move(self): # 方法会出现下划线提示This inspection detects any methods ...
- 【PyCharm编辑器】之无法导入引用手动新建的包或类,报:This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases.
一.现象描述 如下图所示,手动新建个类包calculator.py,想在test.py文件引用它,发现一直报红线,引用失败 Unresolved reference 'calculator' less ...
- this inspection detects names that should resolved but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are sup
输入第一行代码:import logging;logging.basicConfig(level==logging.INFO) 提示:this inspection detects names tha ...
- 解决Pycharm中module 'pip' has no attribute 'main'的问题
背景:pip升级至10.0.1后,使用Pycharm安装Package时一直提示module 'pip' has no attribute 'main'报错信息. 解决方法: 找到Pycharm安装目 ...
- pycharm 提示:this license **** has been cancelled(2)
pycharm安装激活过程中,提示 this license **** has been cancelled .这个问题并不是你的激活码不对,而是需要修改系统的hosts文件,下面详细讲解下如何修改h ...
- This inspection detects shadowing names defined in outer scopes.
错误信息:This inspection detects shadowing names defined in outer scopes. 检查到波浪处的单词已在函数外部定义. 解决:使用global ...
- pycharm安装提示 module 'pip' has no attribute 'main'
问题描述: 环境: windows10 pycharm2016.2.3 //在最先版本的pycharm就没问题,可能还需要升级pip版本 python3.6 pip安装模块,提示 Attribute ...
- Pycharm安装模块提示module 'pip' has no attribute 'main'的问题
解决pycharm问题:module 'pip' has no attribute 'main' 转自: <解决pycharm问题:module 'pip' has no attribute ' ...
- Django | pycharm 提示 unresolved attribute referene 'objects' for class 'xxxx'
objects高亮,提示信息为unresolved attribute referene 'objects' for class 'BookInfo' 当前情况是pycharm没有识别到objects ...
随机推荐
- 【转载】Latex定制章节编号格式和计数器
原文: http://www.chengkaiblog.com/software-application/latex/customize-section-format-counter.html _1. ...
- RabbitMQ channel 参数详解
1.Channel 1.1 channel.exchangeDeclare(): type:有direct.fanout.topic三种durable:true.false true:服务器重启会保留 ...
- [转] Understanding-LSTMs 理解LSTM
图文并茂,讲得极清晰. 原文:http://colah.github.io/posts/2015-08-Understanding-LSTMs/ colah's blog Blog About Con ...
- 从无文件技术到使用隐写术:检查Powload的演变
来源:https://blog.trendmicro.com/trendlabs-security-intelligence/from-fileless-techniques-to-using-ste ...
- python3.7 contextvars在asyncio使用的
from contextvars import ContextVar import asyncio import random cv = ContextVar('cv') async def wait ...
- 题解-洛谷4921&4931 情侣?给我烧了!(加不加强无所谓版)
Problem 简单版 & 加强版 题目概要(其实题面写得很清楚,这里搬运一下): \(n\) 对情侣排座位,恰有 \(n\) 排座位,每排 \(2\) 个座位,在一个就座方案中所有人会将将座 ...
- Linux中error while loading shared libraries错误解决办法
默认情况下,编译器只会使用/lib和/usr/lib这两个目录下的库文件,通常通过源码包进行安装时,如果不指定--prefix,会将库安装在/usr/local/lib目录下:当运行程序需要链接动态库 ...
- frei0r-1.6.1 for win32 133 DLLs
ffmpeg中frei0r滤镜基本使用方法 ffplay -vf frei0r=filter_name=filter_params:filter_params:... 在Windows系统ffmpeg ...
- PHP程序守护进程化
一般Server程序都是运行在系统后台,这与普通的交互式命令行程序有很大的区别.glibc里有一个函数daemon.调用此函数,就可使当前进程脱离终端变成一个守护进程,具体内容参见man daemon ...
- eclipse:显示堆内存
如下图 :