[英]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.