Super(FirstOrderSubClass, self)._init_(*args, **kw)Ĭlass SecondOrderSubClass(FirstOrderSubClass):ĭef _init_(self, name, version, *args, **kw): You can use *args and **kw to capture any additional arguments and just ignore those to make cooperative subclassing work: class BaseClass(object): Since object._init_() never does, this means you need a sentinel class that does not use super() BaseClass will do nicely here. The proper way to use super() is for all your methods to at least accept all the same arguments. Note that there is no point in assigning to self.name there if you are going to ask FirstOrderSubClass._init_ to do the exact same thing. Super(SecondOrderSubClass, self)._init_(name) See *Python's super() is considered super! by Raymond Hettinger, or it's followup P圜on presentation to explain why you want your signatures to match.Ĭalling the BaseClass._init_ unbound method directly (passing in self explicitly) would also work: class SecondOrderSubClass(FirstOrderSubClass): However, your _init_ method signatures do not match so you cannot rely on cooperative behaviour here as soon as you add a mix-in class in the hierarchy, things can and probably will break. You cannot access nfig_array until BaseClass._init_() has run to set the attribute.Įither fix FirstOrderSubClass to also invoke the base class _init_ or call it directly.įixing the FirstOrderSubClass is probably the best way to do so: class FirstOrderSubClass(BaseClass):
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |