Nov-25-2022, 07:39 PM
(This post was last modified: Nov-25-2022, 07:40 PM by Gribouillis.)
(Nov-25-2022, 07:12 PM)Phaze90 Wrote: why I shouldn't inherit from that Base-Class?Metaclasses are for metaprogramming. You don't need that for your first class, so remove the metaclass.
Private attributes with double underscores are more an annoyance than anything else. In Python there is no compiler to enforce more or less privacy on members, so privacy is usually a gentlemen's agreement between the programmer and his/her colleagues. To indicate that an attribute is not in the object's public interface, use a single underscore.
You could write the code this way for example
class MyBaseClass: def __init__(self, material=None, origin=None, manufacturer=None, ) -> None: self._material = material self._origin = origin self._manufacturer = manufacturer class MyClass1(MyBaseClass): def __init__(self, name=None, length=None, height=None, color=None, **kwargs, ) -> None: super().__init__(**kwargs) self._name = name self._length = length self._height = height self._color = color class MyClass2(MyBaseClass): def __init__(self, name=None, vol=None, weight=None, dense=None, **kwargs, ) -> None: super().__init__(**kwargs) self._name = name self._vol = vol self._weight = weight self._dense = dense k1 = MyClass1(name='name', length='leng', height='heig', color='colo', material='mate', origin='orig', manufacturer='manu', ) print(k1) print(vars(k1))
Output:<__main__.MyClass1 object at 0x7fd271dd3ee0>
{'_material': 'mate', '_origin': 'orig', '_manufacturer': 'manu', '_name': 'name', '_length': 'leng', '_height': 'heig', '_color': 'colo'}