Inherit methods from multiple uncooperative classes in Python - python

I've watch Raymond Hettingers Pycon presentation and read his Python’s super() considered super! as well as many questions here on Stackoverflow, but I've encounter a problem with inheritance that I can't seem to find an answer for. I'm going to use my problem as an example, but I'm interested in knowing why it doesn't work rather than how to fix it.
I have 3 classes I want to inherit from, both so that I can use their methods but also so that I can pass an instance of my own class in other functions or methods.
None of the inherited classes calls super().__init__(**kwargs) in their initializer, so I tried making an adapter class for each class, and a Root class to catch the remaining keyword arguments:
class Root(object):
def __init__(**kwargs):
pass
class Rect(Root):
def __init__(self, pos=(0, 0), size=(16, 16), **kwargs):
self.rect = pygame.Rect(pos, size)
super().__init__(**kwargs)
class Sprite(Root):
def __init__(self, **kwargs):
self.sprite = pygame.sprite.Sprite()
super().__init__(**kwargs)
class Surface(Root):
def __init__(self, size, **kwargs):
self.image = pygame.Surface(size).convert()
super().__init__(**kwargs)
class Gameobject(Rect, Sprite, Surface):
def __init__(self, **kwargs):
super().__init__(**kwargs)
This doesn't work because the methods of each class are not inherited to the GameObject class, rather it inherit just instances of each class.
My other attempt was to inherit both Root and their respective class from pygame for each adapter class, for example:
class Rect(Root, pygame.Rect):
def __init__(self, **kwargs):
super().__init__(**kwargs)
This leads to an error: "TypeError: multiple bases have instance lay-out conflict", which I've understood to be a conflict between the built-in implementation or something similar (not quite sure if I understood correctly).
How will I go on from here? Where am I doing wrong? Thanks in advance!

Related

Initialize inherited class outside constructor in Python

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.

Error when using staticmethod with flyweight decorator

I have a class for making sprites flyweight and I am using a decorator to call this class. Here is some code:
class flyweight:
def __init__(self, cls):
self._cls = cls
self.__instances = dict()
def __call__(self, title):
return self.__instances.setdefault((title), self._cls(title))
In this question I'll just simplify the code to show what is relevant.
#flyweight
class Sprite:
def __init__(self, title, surf=None):
self.title = title
self.surf = surf if surf is not None else pygame.image.load('Images/Sprites/'+title+'.png').convert_alpha()
self.w, self.h = self.surf.get_size()
#staticmethod
def from_colour(colour, size=(40,40)):
surf = pygame.Surface(size).convert(); surf.fill(colour)
return Sprite(colour, surf)
red = Sprite.from_colour((125,0,0))
But this gives me the error:
AttributeError: 'flyweight' object has no attribute 'from_colour'
Should I remodel my flyweight implementation or is there some way around this?
Once decorated, the name of the wrapped object automatically points to the returned results of the decorator. In this case, Sprite now stores an instance of flyweight, which in turns contains an attribute storing an instance of the original wrapped class Sprite. For instance, printing Sprite after the declarations gives: <__main__.flyweight object at 0x102373080>. However, the staticmethod from_colour can be called from _cls:
red = Sprite._cls.from_colour((125,0,0))
A flyweight decorator really ought to pass through all constructors to the underlying class, including #classmethod and #staticmethod alternate constructors. In fact, more generally, a class decorator really ought to preserve the wrapped class's entire public interface.
And, while we could easily modify flyweight to specifically pass through the rest of the Sprite interface, which in this case is just that from_colour method, that would be a pain for a less trivial class, or for a class that ever changes. And really, what's the point of making a decorator that only works with a single class?
So, let's change it to:
Take any constructor signature. Ideally we'd want to make it configurable on what part of the signature counts as the key,1 but to keep things from getting too complicated, let's just fix it as the first argument.
Pass through the entire public interface of the class, not just its __call__ interface.
So:
class flyweight:
def __init__(self, cls):
self._cls = cls
self.__instances = dict()
def __call__(self, key, *args, **kw):
return self.__instances.setdefault(key, self._cls(key, *args, **kw))
def __getattr__(self, name):
if not name.startswith('_'):
return getattr(self._cls, name)
1. Some other library I've used has a nice design for this for function memo caches. Probably cachetools. And it ought to make just as much sense for class construction caches.

What is the proper way to deal with Python overriding of class methods with a mixin in multiple inheritance without redundant code?

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?

Subclass of Django Model Not Accessible from Superclass

I'm trying to create an inherited model in Django like that below. I should be able to call, for an instance superclass = Superclass(), I should be able to call superclass.subclass, and access the requisite fields. When I do that, I'm told that '1 argument was expected, and 8 were given': any idea where I'm going wrong?
class Superclass(models.Model):
pass
class Subclass(Superclass):
def __init__(self):
super(Subclass, self).__init__()
The problem here is that you need *args and **kwargs in the subclass constructor and superclass constructor. These two fields will take in the arguments and objects: these include the field information for the superclass, as well as the object manager, among other things. This should do the trick:
class Subclass(Superclass):
def __init__(self, *args, **kwargs):
super(Subclass, self).__init__(*args, **kwargs)

How to refactor those basic classes (PyQt5)

I am learning PyQt5 and I tried to create a extended widgets.
I am also learning OOP so I lack experience for this project.
My final goal is to have master widgets that can disable/enable some slave widgets.
So far I need check and radio buttons.
So I tried to create an abstract class that contains the extended behavior of the widgets (the management of the state of the slaves widgets):
class QDisablingWidget():
__metaclass__ = ABCMeta
def __init__(self):
self.slaveWidgets = []
self.slaveStateWhenMasterIsEnabled = {}
def addSlaveWidget(self, slaveWidget, isEnabledWhenMasterIsEnabled=True):
[...]
def updateSlaveStatus(self):
[...]
Then I create my extended widget classes:
class QDisablingCheckBox(QtWidgets.QCheckBox, QDisablingWidget):
def __init__(self, text=None, parent=None, isInMutexGroup=False):
super(QtWidgets.QCheckBox, self).__init__()
super(QDisablingWidget, self).__init__()
if text:
self.setText(text)
if parent:
self.setParent(parent)
self.isInMutexGroup = isInMutexGroup
# Click signal handling
self.stateChanged.connect(self.updateSlaveStatus)
class QDisablingRadioButton(QtWidgets.QRadioButton, QDisablingWidget):
def __init__(self, text=None, parent=None, isInMutexGroup=False):
super(QtWidgets.QRadioButton, self).__init__()
super(QDisablingWidget, self).__init__()
if text:
self.setText(text)
if parent:
self.setParent(parent)
self.isInMutexGroup = isInMutexGroup
# Click signal handling
self.toggled.connect(self.updateSlaveStatus)
You can already see the problem:
I need to connect my self.updateSlaveStatus to the correct signals (stateChanged and toggled) so I added it in the constructor of the derived classes.
Recently I also added the isInMutexGroup argument for some implementation reasons and I realize that I am duplicating the code in both derived classes...
It is the first time I try to use OOP "for real" (first attempt of multiple inheritance and abstract class), so even if I know I am breaking the beauty of the OOP concept, I don't know what to do to get a nice class hierarchy...
So basically, I am looking for a solution on this example. But I am also looking for guidelines, general advice, tutorials, etc. Actually anything that could help me!
Thank you for your help.
Even if I got a downvote for that question, I think some beginners could be interested by the solution I found:
I have my extension abstract class like that:
class QDisablingWidget(QtCore.QObject):
__metaclass__ = ABCMeta
def __init__(self, isInMutexGroup=False, **kwds):
[...]
Then I can derive class like that:
class QDisablingCheckBox(QtWidgets.QCheckBox, QDisablingWidget):
def __init__(self, **kwds):
super().__init__(**kwds)
# On click signal handling
self.stateChanged.connect(self.updateSlaveStatus)
and
class QDisablingRadioButton(QtWidgets.QRadioButton, QDisablingWidget):
def __init__(self, **kwds):
super().__init__(**kwds)
# On click signal handling
self.toggled.connect(self.updateSlaveStatus)
Finally, when I use the classes I need to create object like that:
disablingRadioBut = QWidgets.QDisablingRadioButton(text="My button",
parent=self,
isInMutexGroup=True)
I.e I must use keywords explicitly so that each constructors will eat the kewords the use/know.
Thanks to this approach I have maximum reusability of my extension class.
I got this solution here:
http://pyqt.sourceforge.net/Docs/PyQt5/multiinheritance.html
And more details here:
http://rhettinger.wordpress.com/2011/05/26/super-considered-super/
A very nice article!

Categories