Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python class Inheritance Initialization

I have the following example setup:

class Feet:
   def __init__ (self, value = 0.0):
      self.value = value
      self.units = "f"

   def feet(self):
      return  self.value


class Meters:
   def __init__(self, value = 0.0):
      self.value = value
      self.units = "m"

   def feet(self):
      # This is probably not an accurate conversion.
      return  self.value * 2.54 * 10 


class Distance (Feet, Meters):
   def __init__(self, type = Feet()):
      Feet.__init__(self)
      Meters.__init__(self)
      print type.feet()   -- Prints 254.0
      self = type
      print self.feet()   -- Prints 254.0

dist = Distance(Meters(10.0))

print dist.units   -- Prints = "m"
print dist.value   -- Prints = 0.0
print dist.feet()  -- Prints = 0.0

I can't seem to understand why when I initialize the class to a Meters Class type, and assign it 10.0, I don't keep the 10.0. However the Units seem to have stayed correct. Am I missing something about how this is being setup?

My understanding is that I'm creating an "instance" of Meters, and assigning it to the "self" variable of Distance. If the self value couldn't be modified I could understand if my units was "f", but my units is "m" so it's obviously assigning the Meters class to self, but it's not taking the instantiated values, which I find quite odd.

To be honest I don't even know what I would google in this case, so I apologize I haven't done a whole lot of googling, most of what I found didn't apply at all to this type of problem.

Additionally, my plan was to basically "cast" it to the same type no matter what you passed in, for example for feet I would return the self instance for the Feet class, and in the Meters class I would return Feet(self.Value * 2.54 * 10) so I would always have my distance in Feet.

so for Feet feet becomes

def feet(self):
   return self

for Meters feet becomes

def feet(self):
  return Feet(self.value * 2.54 * 10)

To Recap, is there a reason that I'm able to pass in 1 of 2 classes as part of initialization, but it doesn't take my initialization parameters for that class?

It's really unclear to me why I can assign "self" in the distance class, and before it returns it appears to have the right initialization but upon returning it doesn't work right.

like image 822
onaclov2000 Avatar asked Nov 30 '25 09:11

onaclov2000


1 Answers

The thing is that you are inheriting from 2 classes Feet and Meters. Both classes have the same methods. In your Distance.__init__() method, you are overriding Feet's methods with Meters' methods when doing this:

  Feet.__init__(self)
  Meters.__init__(self)

What I would have done differently:

class Distance(object):
    def __init__(self, meters=None, feet=None):
        self.feet = feet
        self.meters = meters

Then you can do something like:

distance = Distance(meters=Meters(12))
print distance.meters.value
print distance.meters.type
# Here do whatever you want with them

You can pass in the two objects at the same time. And do some other stuff with the two objects if the are both different than None.

like image 118
Paco Avatar answered Dec 02 '25 21:12

Paco