May-05-2020, 11:34 AM
Show all your code. If you get any traceback - post it verbatim, in error tags.
All that said - you get the thinks the other way around. You should inherit from general class. i.e. you crate a general base class with common methods, then more specific child class(es) will inherit from it, using common methods or overloading some of them. At the moment
Here is an example, the way you do it:
All that said - you get the thinks the other way around. You should inherit from general class. i.e. you crate a general base class with common methods, then more specific child class(es) will inherit from it, using common methods or overloading some of them. At the moment
sphere.surface
will overwrite cube.surface
Here is an example, the way you do it:
from math import pi class Cube: def __init__(self, side): self.side = side @property def surface(self): print('Cube surface method') return 6 * self.side * self.side class Sphere: def __init__(self, radius): self.radius = radius @property def surface(self): print('Sphere surface method') return 4 * pi * self.radius * self.radius class Geometry(Sphere, Cube): def __init__(self, x): # you should use super() to initialize the parent classes Cube.__init__(self, x) Sphere.__init__(self, x) geo = Geometry(5) print(geo.surface) print(Geometry.mro())
Sphere surface method 314.1592653589793 [<class '__main__.Geometry'>, <class '__main__.Sphere'>, <class '__main__.Cube'>, <class 'object'>]as you can see in the Method resolution order Sphere is before Cube
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs