SCRIPTS CLASSES class_exampleΒΆ

# class definitions


class Living(object):
    """
    Base class for all living things
    """

    def __init__(self, alive=True):
        """
        :param alive: Whether living object is alive
        """
        self.alive = alive

    def kill(self):
        """
        Kill this object
        """
        self.alive = False


if False:
    virus = Living()
    print('Virus is alive: ', virus.alive)
    OMFITx.End()


class Mammal(Living):
    """
    Mammal class
    """

    def __init__(self, age=0, **kw):
        """
        :param age: The age of the mammal

        :param \**kw: Additional keywords passed to Living.__init__
        """
        self.age = age
        Living.__init__(self, **kw)


class Person(Mammal):
    """
    Person class
    """

    def __init__(self, occupation='scientist', **kw):
        """
        :param occupation: The occupation of the scientist
        """
        self.occupation = occupation
        Mammal.__init__(self, **kw)

    def __gt__(self, other):
        return self.age > other.age


class Dog(Mammal):
    """
    Dog class
    """


# Class tests
if False:
    print('Dog is subclass of Living: ', issubclass(Dog, Living))
    print('Dog is subclass of Mammal: ', issubclass(Dog, Mammal))
    print('Dog is subclass of Person: ', issubclass(Dog, Person))
    print('Mammal is subclass of Dog: ', issubclass(Mammal, Dog))
    OMFITx.End()

# Object instances
if True:
    protozoa = Living()
    dodo = Living(alive=False)
    Sterling = Person(age=10)
    Orso = Person(age=11, occupation='BDFL')
    Fido = Dog()

    # Instance tests
    if True:
        print('Orso is Sterling: ', Orso is Sterling)
        print('Sterling is instance of Person: ', isinstance(Sterling, Person))
        print('Sterling is instance of Dog: ', isinstance(Sterling, Dog))
        print('Fido is instance of Living: ', isinstance(Fido, Living))
        print('protozoa is alive: ', protozoa.alive)
        protozoa.kill()
        print('protozoa is alive: ', protozoa.alive)
        print('Orso is greater than Sterling: ', Orso > Sterling)
    # help(Orso.__gt__)