Here's the tea: I'm writing a small Monopoly game using python. I've made this little class family to represent the bills. There's a base abstract class called Bill which inherits from the abc.ABC class.
Code:
from abc import ABC, abstractmethod
import colorama
colorama.init(autoreset=True, strip=True)
class Bill(ABC):
#Abstract Properties (must be overriden in subclasses)
#property
#abstractmethod
def count(self):
return 0
#property
#abstractmethod
def color(self): # colorama background color
return 0
#property
#abstractmethod
def worth(self): # integer representing the bill's worth
return 0
# Dunder methods (The same across class family)
def __add__(self, other):
return self.worth + other.worth
def __str__(self):
return f" {self.color} {self.worth}"
class One(Bill):
def __init__(self):
self.__count = 0
self.__color = "\033[0m"
self.__worth = 0
#super().init() ??
# Override Abstract Methods
#property
def count(self):
return self.__count
#count.setter
def count(self, amount):
self.__count += 1
#property
def worth(self):
return 1
#property
def color(self):
return colorama.Back.WHITE
My question is whether my subclasses will inherit the Bill class's dunder methods.
And if not, do I need to include super().__init__() in the One class's __init__ method?
Related
How to access the variables of the outer class in the Inner Class?
class Student:
def __init__(self,Name,rollNumber):
self.Name=Name
self.rollNumber=rollNumber
self.lap=self.Laptop()
def Show(self):
print(self.Name)
print(self.lap.show())
class Laptop:
def __init__(self):
self.brand = "Mac"
self.cpu = "i9"
self.ram = 16
def show(self):
return self.brand
#staticmethod
def Show():
return s1.Name
s1=Student("Teja",2)
print(s1.Name,s1.rollNumber)
s1.Show()
print(s1.lap.brand)
system=s1.lap
print(system.brand)
print(system.cpu)
print(system.show())
print(system.Show())
I have two class one is Vehicle and one is BMW, I know I can create a color as property unver Vehicle. However how I set the property inside class BMW
but i am still able to access color as bmw.info,color? Can I do something like following?
class Vehicle:
def __init__(self, name):
self.name = name
self._color = None
#property
def color(self):
return self._color
#color.setter
def color(self, color):
self._color = color
class BMW:
def __init__(self, name):
self.info = Vehicle(name)
#can I do something like
class Vehicle:
def __init__(self, name):
self.name = name
class BMW:
def __init__(self, name):
self.info = Vehicle(name)
setattr(self.info, "color", None)
#info.property
def color(self):
return self._color
#info.color.setter
def color(self, color):
self._color = color
bmw = BMW('sister')
print(bmw.info.color)
bmw.info.color = 'red'
print(bmw.info.color)
is it possible to change the method resolution order?
class A(object):
def __init__(self, a):
self.a=a
def method(self):
print('A method')
class B(object):
def __init__(self, b1, b2):
self.b1=b1
self.b2=b2
def method(self):
print('B method')
class C(A, B):
def __init__(self, name, **kwargs):
if name=='A':
A.__init__(self, a=kwargs['a'])
elif name=='B':
B.__init__(self, b1=kwargs['b1'], b2=kwargs['b2'])
I want change the MRO if the input name is 'B', so that when i call:
>>>c=C(name='B', b1=2, b2=3)
>>>c.method()
it returns 'B method'.
Thanks
Just change the definition of the C class to :
class C(B, A):
...
ie : change the order of the inherited classes.
What is the simplest / most pythonic way to override only the setter of an abstract property in Python 3? Variant 3 seems to mean the least effort for the derived class implementor. Is it correct? Does it have disadvantages?
import abc
class A1(metaclass=abc.ABCMeta):
def __init__(self, x, **kwargs):
super().__init__(**kwargs)
self._x = x
#property
def x(self):
return self._x
#x.setter
#abc.abstractmethod
def x(self, value):
self._x = value
class B1(A1):
#property
def x(self):
return super().x
#x.setter
def x(self, value):
print("B1 setter")
super(B1, self.__class__).x.fset(self, value)
b1 = B1(x=1)
b1.x = 3
print(b1.x)
class A2(metaclass=abc.ABCMeta):
def __init__(self, x, **kwargs):
super().__init__(**kwargs)
self._x = x
#abc.abstractmethod
def _get_x(self):
return self._x
#abc.abstractmethod
def _set_x(self, value):
self._x = value
x = property(_get_x, _set_x)
class B2(A2):
def _get_x(self):
return super()._get_x()
def _set_x(self, value):
print("B2 setter")
super()._set_x(value)
x = property(_get_x, _set_x)
b2 = B2(x=1)
b2.x = 3
print(b2.x)
class A3(metaclass=abc.ABCMeta):
def __init__(self, x, **kwargs):
super().__init__(**kwargs)
self._x = x
def _get_x(self):
return self._x
#abc.abstractmethod
def _set_x(self, value):
self._x = value
x = property(
lambda self: self._get_x(),
lambda self, value: self._set_x(value))
class B3(A3):
def _set_x(self, value):
print("B3 setter")
super()._set_x(value)
b3 = B3(x=1)
b3.x = 3
print(b3.x)
So, yes, you listed a lot of ways in there - and although the one that requires more code is your variant 3, the most straighforard, least surprising way to do it is your variant 1 -
It just works, and is perfectly readable, no surprises - and there seems to be no simpler way than calling fget explicitly there.
I'd like to create a new class that inherits two subclasses of QWidget. I know multi-inheritance isn't possible in pyqt, but how could I manage to have the properties of both parent classes in one subclass?
What I wish I could do is as follows:
class A(QWidget):
def __init__(self, widget, parent=None):
widget.destroyed.connect(self.destroy_handler)
#pyqtSlot()
def destroy_handler(self):
pass
class B (A, QStatusBar):
def __init__(self, widget, parent=None):
A.__init__(self, widget)
QStatusBar.__init__(self, parent)
#pyqtSlot()
def destroyed_handler(self):
print("Destroyed")
I finally found how to do it: first of all, the problems came from A and QStatusBar inheriting QWidget. We can't change QStatusBar, so we must changer A.
A shouldn't inherit QWidget: so let's create another class, AInterface, like that:
class AInterface(QObject):
def __init__(self, a, parent=None)
super().__init__(parent=parent)
self.a = a
self.connect_signal()
def connect_signal(self, widget):
widget.destroyed.connect(self.handler)
#pyqtSlot()
def handler(self):
self.a.handler()
A has now the following implementation:
class A:
def __init__(self, widget):
a.widget = widget
a.interface = AInterface(self)
def handler(self):
pass
Thus, now we can create subclasses inheriting not only A but also any QObject, like this:
class B(QStatusBar, A):
def __init__(self, widget, parent=None):
QStatusBar.__init__(self, parent=parent, wiget=widget)
A.__init__(self, widget)
def handler(self):
self.show('Destroyed', 3000)
Notice the widget=widget in the constructor of QStatusBar: if we don't specify it, a TypeError is thrown...