简体   繁体   English

未调用基类'*'的Pylint错误__init__方法

[英]Pylint error __init__ method from base class '*' is not called

I have an pylint error: 我有一个pylint错误:

__init__ method from base class 'night' is not called

I have a file called day_night.py: 我有一个名为day_night.py的文件:

// is_it_night returns True False depending on the time
import is_it_night

class Day():

   def __init__(self):
       // dosomething

class Night():

    def __init__(self):
        // dosomethingdifferent   
what_time = Day

if is_it_night():
    what_time = Night

Now I have a class called instance.py which looks like this: 现在,我有一个名为instance.py的类,它看起来像这样:

from day_night import what_time

class instance(day_night):

   def __init__(self):
      what_time.__init__(self)

And now I get the pylint error on incstance.py that 现在我在incstance.py上收到pylint错误

 __init__ method from base class 'night' is not called

How do I fix this, I tried super but that creates other problems 我该如何解决这个问题,我尝试了超级,但这会带来其他问题

if i do instance.py 如果我做instance.py

from day_night import what_time

class instance(day_night):

   def __init__(self):
      super(instance, self).__init__(self)

I get the following error: use super on old style class 我收到以下错误:在旧样式类上使用super

And I would prefer not to do # pylint: disable= 而且我不想做#pylint:disable =

Your problem seems to be 你的问题似乎是

from day_night import what_time

class instance(day_night):
   what_time.__init__(self)

Here 这里

from day_night import what_time

you import the class what_time , and from this you should derive. 您导入了what_time类,您应该从中派生。

So do 也是

class instance(what_time): # instance is wrongly named...
    def __init__(self):
        super(instance, self).__init__() # here you do the requested call

or, if you cannot change your classes to new.style classes for some reason (just derive from object additionally), you can do 或者,如果由于某种原因而无法将类更改为new.style类(仅从object派生),则可以执行

class instance(what_time): # instance is wrongly named...
    def __init__(self):
        what_time.__init__(self) # here you do the requested call

Or, as m170897017 suggests, omit the __init__ altogether: 或者,按照m170897017的建议,完全省略__init__

class instance(what_time): # instance is wrongly named...
    pass

From my understanding, you want to do something in Day/Night initialization from instance.py. 据我了解,您想在instance / py中进行日/夜初始化。

Change code in instance.py to: 将instance.py中的代码更改为:

from day_night import what_time

class myInstance(what_time):
    pass
    # do something, other than defining a __init__() function
m = myInstance()

If you don't define a __init__() in subclass, when it's instancing, __init__() from baseclass will be invoked. 如果未在子类中定义__init__() ,则在实例化时,将调用基类中的__init__()

Tell me if it works. 告诉我是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 抽象类的错误“未调用基类的 __init__ 方法” - Error "__init__ method from base class is not called" for an abstract class Pylint 说:W0233:调用来自非直接基类“Nested”的 __init__ 方法(​​非父初始化调用) - Pylint says: W0233: __init__ method from a non direct base class 'Nested' is called (non-parent-init-called) Pylint错误W0232:类没有__init__方法 - Pylint error W0232: class has no __init__ method 在基类__init__的子类__init__之后调用基类方法? - Calling base class method after child class __init__ from base class __init__? 从派生类自动调用基类__init__ - Base Class __init__ being called automatically from derived class 从基类的__init__中意外调用ovverriden方法 - Accidentally calling an ovverriden method from base class's __init__ PySide.QtGui RuntimeError: '__init__' 对象基类的方法未调用...但它是 - PySide.QtGui RuntimeError: '__init__' method of object's base class not called ...but it was 更改在基类的 __init__ 中调用的函数的签名 - Change signature of function called in __init__ of base class __init__是一种类方法吗? - Is __init__ a class method? 如何从基类中引用Python中派生类的__init__方法? - How can I reference the __init__ method of a derived class in Python from the base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM