[Tutor] python varying mulitple inheritance

kendy at kendy.org kendy at kendy.org
Mon Jun 18 01:38:57 CEST 2012


Hello

I'm new to classes. And I hope the my question isn't too big.

I have electronic test equipment, but thought that a boat example
would be easier. Can you help me untangle my class design?
My problem is basically multiple inheritance, but I want to be
flexible for how many will inherit.

I'll put my example below and also as an attachment.

I'm running Python 2.7 on Linux. Python is the BEST!

Thanks
Ken

------------ cut here ------------
#!/usr/bin/env python
"""
    Variable Multiple Inheritance.
    email: kendy at kendy.org

    A boat has a body and is powered by any combination 
    of Battery and/or Solar and/or Gas or None.

    If a boat is powered, it should have only one emergency_shutoff.
"""

class Body:
    """ A boat has a body """
    def __init__(self, length):
        self.length = length


class Power:
    """ 
        Super Base class
        If a boat is powered. 
    """
    def __init__(self, emergency_shutoff):
        self.emergency_shutoff = emergency_shutoff

    def Emergency():
        emergency_shutoff = True


class Battery(Power):
    """ sub Child class """
    def __init__(self, emergency_shutoff, battery_volt):
        self.battery_volt = battery_volt
        Power.__init__(self, emergency_shutoff)

    def ChargeBattery():
        pass

class Solar(Power):
    """ sub Child class """
    def __init__(self, emergency_shutoff, solar_volt):
        self.solar_volt = solar_volt
        Power.__init__(self, emergency_shutoff)

    def DoSomethingSolar():
        pass

class Gasoline(Power):
    """ sub Child class """
    def __init__(self, emergency_shutoff, liters):
        self.liters = liters
        Power.__init__(self, emergency_shutoff)

class Boat1(Body):
    """ Sailboat """
    pass

class Boat2(Body, Gasoline, Battery):
    """ Gas, with battery backup powered """
    def __init__(self, length, emergency_shutoff, liters, battery_volt):
        Body.__init__(self, length)
        Power.__init__(self, emergency_shutoff)
        Gasoline.__init__(self, emergency_shutoff, liters)
        Battery.__init__(self, emergency_shutoff, battery_volt)

class Boat3(Body, Battery, Solar):
    """ Electric powered """
    def __init__(self, length, emergency_shutoff, solar_volt, battery_volt):
        Body.__init__(self, length)
        Power.__init__(self, emergency_shutoff)
        Solar.__init__(self, emergency_shutoff, solar_volt)
        Battery.__init__(self, emergency_shutoff, battery_volt)


def ShowAssembly(assembly_queue):
    for workon in assembly_queue:
        pass


def main():
    """ Acts like a container class"""
    assembly_queue = []  # A list of boats to be assembled, in order.

    assembly_queue.append(Boat2(30, False, 'half-full', 50))
    assembly_queue.append(Boat3(25, False, 30, 10))
    assembly_queue.append(Boat1(20))

    ShowAssembly(assembly_queue)


if __name__ == '__main__': 
    main()

------------ cut here ------------

-------------- next part --------------
A non-text attachment was scrubbed...
Name: vary_mult_inherit.py
Type: application/octet-stream
Size: 2454 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20120617/206d2854/attachment.obj>


More information about the Tutor mailing list