示例代码:

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的更多相关文章

  1. pycharm提示This inspection detects any methods which may safely be made static.

    示例代码: class Car(object): # 未定义任何类属性 def move(self): # 方法会出现下划线提示This inspection detects any methods ...

  2. 【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 ...

  3. 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 ...

  4. 解决Pycharm中module 'pip' has no attribute 'main'的问题

    背景:pip升级至10.0.1后,使用Pycharm安装Package时一直提示module 'pip' has no attribute 'main'报错信息. 解决方法: 找到Pycharm安装目 ...

  5. pycharm 提示:this license **** has been cancelled(2)

    pycharm安装激活过程中,提示 this license **** has been cancelled .这个问题并不是你的激活码不对,而是需要修改系统的hosts文件,下面详细讲解下如何修改h ...

  6. This inspection detects shadowing names defined in outer scopes.

    错误信息:This inspection detects shadowing names defined in outer scopes. 检查到波浪处的单词已在函数外部定义. 解决:使用global ...

  7. pycharm安装提示 module 'pip' has no attribute 'main'

    问题描述: 环境: windows10 pycharm2016.2.3  //在最先版本的pycharm就没问题,可能还需要升级pip版本 python3.6 pip安装模块,提示 Attribute ...

  8. Pycharm安装模块提示module 'pip' has no attribute 'main'的问题

    解决pycharm问题:module 'pip' has no attribute 'main' 转自: <解决pycharm问题:module 'pip' has no attribute ' ...

  9. Django | pycharm 提示 unresolved attribute referene 'objects' for class 'xxxx'

    objects高亮,提示信息为unresolved attribute referene 'objects' for class 'BookInfo' 当前情况是pycharm没有识别到objects ...

随机推荐

  1. python 08

    函数 函数定义: 你可以定义一个由自己想要功能的函数,以下是简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号(). 任何传入参数和自变量必须放在圆括号中间.圆括号之间可以用于 ...

  2. Can not deserialize instance of xxx out of START_ARRAY token

    Json 反序列化异常 Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableEx ...

  3. 转:springmvc常用注解标签详解

    Spring5:@Autowired注解.@Resource注解和@Service注解 - IT·达人 - 博客园--这篇顺序渐进,讲得超级好--此人博客很不错http://www.cnblogs.c ...

  4. Android视频压缩

    最推荐(使用方便,默认压缩为原来视频大小的1/4左右): https://blog.csdn.net/qq_35373333/article/details/79564991  https://git ...

  5. 20165237 2017-2018-2 《Java程序设计》第四周考试补做及2-3章编程题

    20165237 2017-2018-2 <Java程序设计>第四周考试补做及2-3章编程题 测试JDB: 用JDB调试上一个程序,输入1.2.3: 2-3章编程题代码托管 (程序的运行结 ...

  6. 修改/dev/shm大小 tmps

    /dev/shm  在oracle开启AMM时发现/dev/shm下的空间不够用 WARNING: You are trying to use the MEMORY_TARGET feature. T ...

  7. RF运行脚本报错:WebDriverException: Message: unknown error: call function result missing

    原因:浏览器驱动与浏览器版本不对应

  8. MySQL MyISAM引擎转换为InnoDB操作记录

    进入mysql命令行模式: # mysql -uroot -ppwd 1.查看mysql提供什么存储引擎: mysql> show engines; 2.查看mysql当前提供的默认存储引擎: ...

  9. Python2018-列表的相关操作

    列表中存放的数据是可以进行修改的,比如"增"."删"."改" .“查” "增"-----append, extend, ...

  10. iOS9 新功能:Support Universal Links,iOS10 openUrl新函数

    先看官方文档:https://developer.apple.com/library/ios/documentation/General/Conceptual/AppSearch/UniversalL ...