Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inheritance problem
#2
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 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

Reply


Messages In This Thread
Inheritance problem - by DPaul - May-05-2020, 10:04 AM
RE: Inheritance problem - by buran - May-05-2020, 11:34 AM
RE: Inheritance problem - by DPaul - May-05-2020, 03:01 PM
RE: Inheritance problem - by buran - May-05-2020, 03:46 PM
RE: Inheritance problem - by deanhystad - May-05-2020, 04:01 PM
RE: Inheritance problem - by buran - May-05-2020, 04:08 PM
RE: Inheritance problem - by DPaul - May-05-2020, 04:14 PM
RE: Inheritance problem - by buran - May-05-2020, 04:22 PM
RE: Inheritance problem - by deanhystad - May-05-2020, 04:42 PM
RE: Inheritance problem - by DPaul - May-05-2020, 05:46 PM
RE: Inheritance problem - by buran - May-05-2020, 06:37 PM
RE: Inheritance problem - by deanhystad - May-05-2020, 08:13 PM
RE: Inheritance problem - by DPaul - May-06-2020, 06:35 AM
RE: Inheritance problem - by buran - May-06-2020, 06:44 AM
RE: Inheritance problem - by DPaul - May-06-2020, 07:38 AM
RE: Inheritance problem - by buran - May-06-2020, 07:46 AM
RE: Inheritance problem - by DPaul - May-06-2020, 05:30 PM
RE: Inheritance problem - by buran - May-06-2020, 05:35 PM
RE: Inheritance problem - by DPaul - May-07-2020, 10:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Inheritance problem Maryan 0 1,313 Oct-25-2020, 02:39 PM
Last Post: Maryan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020