I have something like this:
from some_module import SomeClass
class BaseClass:
...
class AnotherClass(BaseClass, SomeClass):
def __init__(self, parameter, **kwargs):
BaseClass.__init__(**kwargs)
self.parameter
def some_abstract_method(self, **kwargs):
# this is an abstract method declared in BaseClass
# and the arguments **kwargs must be passed here
# to respect the interface
SomeClass.__init__(self.parameter, **kwargs)
SomeClass.do_something()
return self
The reason I am doing this is to respect the interface of BaseClass. I am wondering how bad is it to structure the object AnotherClass like this and if there is a better way to achieve what I am trying to do. The only other way I can think of is to do something like this:
class AnotherClass(BaseClass):
def __init__(self, parameter, **kwargs):
super().__init__(**kwargs)
self.parameter
def some_abstract_method(self, **kwargs):
self.model = SomeClass(self.parameter, **kwargs)
self.model.do_something()
return self.model
But I don't like it because AnotherClass is essentially the same object has SomeClass. Basically, I wonder what the best practice would be here.
I always prefer composition over inheritance if I can. The reasons are manifold, but let's try and numerate a few ones:
less state on the surrounding object.
not violating the single concern paradigm.
not risking inadvertent name clashes.
easier to test.
looser coupling.
So I would roll with your second solution if you can.
Related
I have an existing class TSEparser that inherits from a parent class Subparser, but now I want to add another parent class (SubparserMixin) to class TSEparser. However, the problem is that the arguments passed by the initial inheritance gets in the way of this new inheritance- how can I fix this?
i.e:
class Subparser:
def __init__(self, filename, data_group, **kwargs):
self.data_group = data_group
self.filename = filename
self.__dict__.update(kwargs)
class TSEparser(Subparser):
def __init__(self, filename, data_group, **kwargs):
super().__init__(filename, data_group, **kwargs)
Now I want to add another parent class SubparserMixin so we have class TSEparser(Subparser, SubparserMixin), however Subparsermixin looks like this:
class SubparserMixin:
def __init__(self):
self.subparsers = {}
self.context = PacketContext
Is there some way I can inherit separately from both Parent Classes? Something like this:
class TSEparser(Subparser):
def __init__(self, filename, data_group, **kwargs):
super(Subparser).__init__(filename, data_group, **kwargs)
super(SubparserMixin).__init__()
I know the syntax is not correct but I hope it is clear what I am trying to do!
You can specify which class' constructor gets called by simply using its name. So in your example you can just do
class TSEparser(Subparser, SubparserMixin):
def __init__(self, filename, data_group, **kwargs):
Subparser.__init__(self, filename, data_group, **kwargs)
SubparserMixin.__init__(self)
There is no good and easy way to make this work using super. You can check out this question for more detail. super normally takes care of calling the proper classes for you. If you want to call the __init__ of a specific class manually (e.g. because the required arguments are different), you need to call it directly using its name as shown above.
Edit: My code example mistakenly didn't pass self when calling the base __init__ methods. Note that this is a difference between calling by super and directly by base class. super doesn't require self, while direct calling does.
# without self
super().__init__()
# with self
Base.__init__(self)
I have a class design where the Children classes inheriting from a certain Parent class just differ in some parameters, but the Parent class contains all methods, which are using the parameters provided as class variables on the Children. So, in other words, each of my Child classes is fully described by the list of parameters and the inheritance of the Parent class.
So, let's say, I have the following classes:
class Parent():
def __init__(self, **kwargs):
for param in self.__class__.parameters:
self.setattr(param, kwargs.get(param))
def compare(self, other):
for param in self.__class__.parameters:
if self.getattr(param) != other.getattr(param):
return False
return True
class ChildA(Parent):
parameters = ["length", "height", "width"]
def __init__(self, **kwargs):
super().__init__(**kwargs)
class ChildB(Parent):
parameters = ["color", "taste"]
def __init__(self, **kwargs):
super().__init__(**kwargs)
My actual classes are a bit different - I have more and more complex methods on the Parent class and also different kinds of parameters - , but this is sort of a minimum example of the design principle.
Since Parent class is relying on its Children to have the class variable parameters, I thought, I might want to enforce the existence of the class variable on each Child class. I have read that I achieve this by using a metaclass. But I have also read that most developers do not need to use metaclasses, and if in doubt, you probably don't need them. I have never worked with metaclasses before, and so I am in doubt whether I should use them, and so by that rule mentioned, I probably do not need a metaclass. But on the other hand, the term "metaclass" just sounds like a good match to my structure, since Parent really looks like something which could well be called "metaclass" in some sense (technically, not in terms of the way the terminus technicus metaclass is used in OOP, but in terms of: it is fully describing the behaviour of the children classes).
So, I wonder: Is there a different (better) design of classes to reflect my structure? Should I use a metaclass to enforce the existence of the parameters, or is there a better way to do so? Or should I just resign to enforce the existence of the parameters class variable on the Children classes in the first place?
If using python3.6 or above, you can accomplish this using __init_subclass__ which I personally reason better with than a metaclass.
An example of __init_subclass__ based on the usecase described:
class Parent:
def __init_subclass__(cls):
if not hasattr(cls, 'parameters'):
raise TypeError(f'Subclass of {cls} does not have a parameters class attribute')
def __init__(self, **kwargs):
for param in self.__class__.parameters:
self.setattr(param, kwargs.get(param))
def compare(self, other):
for param in self.__class__.parameters:
if self.getattr(param) != other.getattr(param):
return False
return True
class GoodChild(Parent):
parameters = ['length', 'height', 'width']
class BadChild(Parent):
pass
Which results in raising a TypeError exception when the BadChild class is created (not when it is instantiated):
TypeError: Subclass of <class '__main__.BadChild'> does not have a parameters class attribute
I'm having a minor, I hope, issue with theory and the proper way to deal with a problem. It's easier for me to show an example then to explain as I seem to fail with my vocabulary.
class Original_1:
def __init__(self):
pass
def meth1(self):
pass
def meth2(self):
pass
class Original_2(Original_1):
def __init__(self):
Original_1.__init__(self)
def meth3(self):
pass
class Mixin:
def __init__(self):
pass
def meth4(self):
...
meth1(self)
meth2(self)
class NewClass_1(Original_1, Mixin):
def __init__(self):
Original_1.__init__(self)
Mixin.__init__(self)
class NewClass_2(Original_2, Mixin):
def __init__(self):
Original_2.__init__(self)
Mixin.__init__(self)
Now the goal is to extend Original_1 or Original_2 with new methods in the Mixin, but I run into some questions if I use meth1(), meth2(), or meth3() in the mixin. 1. I'm not referencing Original_1 or Origninal_2 in the mixin. (At this point it runs but I don't like it.) 2. If I make Mixin a child of Original_1, it breaks. I could make two separate NewClass_X but then I'm duplicating all of that code.
Mixins are used to add functionality (usually methods) to classes by using multiple inheritance.
For example, let's say you want to make a class's __str__ method return everything in uppercase. There are two ways you can do this:
Manually change every single class's __str__ method:
class SomeClass(SomeBase):
def __str__(self):
return super(SomeClass, self).__str__().upper()
Create a mixin class that does only this and inherit from it:
class UpperStrMixin(object):
def __str__(self):
return super(UpperStrMixin, self).__str__().upper()
class SomeClass(SomeBase, UpperStrMixin):
...
In the second example, notice how UpperStrMixin is completely useless as a standalone class. Its only purpose is to be used with multiple inheritance as a base class and to override your class's __str__ method.
In your particular case, the following will work:
class Mixin:
def __init__(self, option):
...
def meth4(self):
...
self.meth1()
self.meth2()
class NewClass_1(Original_1, Mixin):
def __init__(self, option):
Original_1.__init__(self)
Mixin.__init__(self, option)
...
class NewClass_2(Original_2, Mixin):
def __init__(self, option):
Original_2.__init__(self)
Mixin.__init__(self, option)
...
Even though Mixin.meth1 and Mixin.meth2 aren't defined, this isn't an issue because an instance of Mixin is never created directly and it's only used indirectly through multiple inheritance.
Since Mixin is not a standalone class, you can just write it to assume that the necessary methods exist, and it will find them on self assuming the self in question provides, or derives from another class which provides, meth1 and meth2.
If you want to ensure the methods exist, you can either document it in the Mixin docstring, or for programmatic enforcement, use the abc module to make Mixin an ABC and specify what methods must be defined; if a given class doesn't provide them (directly or via inheritance) then you'll get an error if you attempt to instantiate it (because the class is still abstract until those methods are defined):
from abc import ABCMeta, abstractmethod
class Mixin(metaclass=ABCMeta):
def __init__(self):
pass
#abstractmethod
def meth1(self): pass
#abstractmethod
def meth2(self): pass
def meth4(self):
...
self.meth1() # Method call on self will dispatch to other class's meth1 dynamically
self.meth2() # Method call on self will dispatch to other class's meth2 dynamically
Beyond that, you can simplify your code significantly by using super appropriately, which would remove the need to explicitly call the __init__s for each parent class; they'd be called automatically so long as all classes use super appropriately (note: for safety, in cooperative inheritance like this, you usually accept the current class's recognized arguments plus varargs, passing the varargs you don't recognize up the call chain blindly):
class Original_1:
def __init__(self, orig1arg, *args, **kwargs):
self.orig1val = orig1arg # Use what you know
super().__init__(*args, **kwargs) # Pass what you don't
def meth1(self):
pass
def meth2(self):
pass
class Original_2(Original_1):
def __init__(self, orig2arg, *args, **kwargs):
self.orig2val = orig2arg # Use what you know
super().__init__(self, *args, **kwargs) # Pass what you don't
def meth3(self):
pass
class Mixin(metaclass=ABCMeta):
# If Mixin, or any class in your hierarchy, doesn't need to do anything to
# be initialized, just omit __init__ entirely, and the super from other
# classes will skip over it entirely
def __init__(self, mixinarg, *args, **kwargs):
self.mixinval = mixinarg # Use what you know
super().__init__(self, *args, **kwargs) # Pass what you don't
#abstractmethod
def meth1(self): pass
#abstractmethod
def meth2(self): pass
def meth4(self):
...
self.meth1() # Method call on self will dispatch to other class's meth1
self.meth2() # Method call on self will dispatch to other class's meth1
class NewClass_1(Original_1, Mixin):
def __init__(self, newarg1, *args, **kwargs):
self.newval1 = newarg1 # Use what you know
super().__init__(self, *args, **kwargs) # Pass what you don't
class NewClass_2(Original_2, Mixin):
def __init__(self, newarg2, *args, **kwargs):
self.newval2 = newarg2 # Use what you know
super().__init__(self, *args, **kwargs) # Pass what you don't
Note that using super everywhere means you don't need to explicitly call each __init__ for your parents; it automatically linearizes the calls, so for example, in NewClass_2, that single super().__init__ will delegate to the first parent (Original_2), which then delegates to Original_1, which then delegates to Mixin (even though Original_1 knows nothing about Mixin).
In more complicated multiple inheritance (say, you inherit from Mixin through two different parent classes that both inherit from it), using super is the only way to handle it reasonably; super naturally linearizes and deduplicates the parent class tree, so even though two parents derive from it, Mixin.__init__ would still only be called once, preventing subtle errors from initializing Mixin more than once.
Note: You didn't specify which version of Python you're using. Metaclasses and super are both better and simpler in Python 3, so I've used Python 3 syntax. For Python 2, you'd need to set the metaclass a different way, and call super providing the current class object and self explicitly, which makes it less nice, but then, Python 2 is generally less nice at this point, so consider writing new code for Python 3?
I have a class Document, this class is really complex to instantiate so I have a builder object to create them. Both elements are not mine, so I can't change them
Now, I want to create a subclass of Document, just to add some specific methods. In order to keep using the provided builder I tried this:
class SpecialDocument(Document):
def __new__(cls, *args):
return DocumentBuilder(*args)
def __init__(self, *args, **kwargs):
#My initialization
The problem here is that the __init__ method never executes cause the __new__ method doesn't return a SpecialDocument (It returns a Document)
In my particular case I don't need to build my SpecialDocument differently from how I build a Document. Is there a way to use the same builder? If not, how can I achieve this? I just want to inherit from Document to add particular functionalities, maybe it could be achieved with metaclasses but I never used them (Probably cause I don't fully understand it), a little insight on them would be nice if it can help solving my problem
You don't actually need a metaclass here - you just have to proper call the superclass' __new__ method. The way you are doing it, the instantiation of the superclass does not "know" it is being called from a subclass at all.
So, just write your code like this instead:
class SpecialDocument(Document):
def __new__(cls, *args):
return super().__new__(cls, *args)
def __init__(self, *args, **kwargs):
#My initialization
Now, that is the ordinary way to do it - and would work if the code in your "builder" function was correctly placed inside Docment's __new__ or __init__.
Since the code there does nt do that, and you can[ t pass your subclass as a parameter to the builder, a working solution might be to create a normal document, and swap its class after it has been built:
def special_document_init(special_document):
...
class SpecialDocument(Document):
def my_special_method(self, ...):
...
def overriden_method(self):
...
result = super().overriden_method()
...
def build_special_document(*args):
document = DocumentBuilder(*args)
document.__class__ = SpecialDocument
special_document_init(document)
return document
I have a classes BasicEvidenceTarget and SchedulableSoma. Sometimes I inherit from SchedulableSoma, BasicEvidenceTarget, sometimes I inherit from SchedulableSoma alone. When I inherit from SchedulableSoma, BasicEvidenceTarget, I want SchedulableSoma to override the method BasicEvidenceTarget.inject_basic_evidence. What's a nice way of doing that?
The override looks like this:
class SchedulableSoma(SchedulableCluster, Soma):
# This is a possible overload of this method in BasicEvidenceTarget.
def inject_basic_evidence(self, *args, **kwargs):
super().inject_basic_evidence(*args, **kwargs)
self.ask_for_reschedule()
Right now I am unconditionally overriding the base class method, and so if it doesn't exist, there is a bit of method pollution: if the override is called, the call super will fail. It would be nicer to conditionally generate the override.
I feel like there is a __prepare_subclass__ magic that might work, but I'm not sure exactly how to do it.
You could use a factory function to generate an appropriate class at the time you define the class that is inheriting it. This is just a sketch:
def makeSubclass(otherclass):
class MixedClass(otherclass):
if otherClass == BasicEvidenceTarget:
def inject_basic_evidence(...):
# ...
elif otherClass == WhateverOtherClass:
def some_other_method(...):
# ...
Then you would do:
class SchedulableSoma(makeSubclass(BasicEvidenceTarget)):
# ...
Another possibility is simply to define all the methods in SchedulableSoma, but include checks in them that raise exceptions if the current instance does not inherit from the appropriate class. Something like:
class SchedulableSoma(object):
def inject_basic_evidence(self, *args, **kwargs):
if not isinstance(self, BasicEvidenceTarget):
raise TypeError("Cannot call inject_basic_evidence unless you inherit from BasicEvidenceTarget")
def some_other_method(self, *args, **kwargs):
if not isinstance(self, SomeOtherClass):
raise TypeError("Cannot call some_other_method unless you inherit from SomeOtherClass")
# similar checks for other classes
This way the call to inject_basic_evidence will fail right away with a more specific error message, rather than failing on the super call with a more obscure message about "super object has no attribute" or the like.
Ultimately you might want to think about whether there is a more robust way to structure your class hierarchy. It is somewhat magical to have classes alter their own behavior depending on whether certain other classes also appear in the inheritance hierarchy, and it may confuse users or lead to unforeseen interactions among the classes down the road.
What you attempting to do is impossible at class definition time without funcy metaclass business. This could be done at instance creation time with the following code:
class SchedulableSoma(SchedulableCluster, Soma):
def __init__(self,*args,**kwargs):
super().__init__(self,*args,**kwargs)
if hasattr(self,"inject_basic_evidence"):
def inject_basic_evidence(*args, **kwargs):
super().inject_basic_evidence(*args, **kwargs)
self.ask_for_reschedule()
self.inject_basic_evidence = inject_basic_evidence
NOTE: This does not work if a subclass of SchedulableSoma overrides inject_basic_evidence
I think that this problem could be solved by different inheritence schema. Using your idea:
class YourClass(SchedulableSoma, BasicEvidenceTarget):
def inject_basic_evidence(self, *args, **kwargs):
if isinstance(self, BasicEvidenceTarget):
BasicEvidenceTarget.inject_basic_evidence(self, *args, **kwargs)
else:
super().inject_basic_evidence(*args, **kwargs)
self.ask_for_reschedule()
Or
class YourClass(SchedulableSoma, BasicEvidenceTarget):
if issubclass(YourClass, BasicEvidenceTarget):
def inject_basic_evidence(self, *args, **kwargs):
super().inject_basic_evidence(*args, **kwargs)
self.ask_for_reschedule()