david
david

Reputation: 6805

Understanding parent/child classes in Python

I am learning Python, and I just came a cross an example on youtube, which is confusing me for a number of reasons. The first one being this.. It is my understanding that when creating a class, anything in the parenthesis after, must either be blank or a parent class. Meaning the class being created is inheriting things from a different class. For example:

Class Child(Parent):

In the example pasted below, the first class being created here has 'Object' in the parenthesis, which I don't understand what that is or what it's referencing because I don't see this anywhere else in the code and there is certainly not a class named 'Object'.

#http://www.youtube.com/watch?v=OcKeDVOzTwg

import sys

YELLOW= '\033[93m'
RED = '\033[91m'
NORMAL = '\033[0m'

Class Person(object):
    def __init__(self, name, age):
        self.name=name
        self.age=age

    def __str__(self):
        return %s is %d (self.name, self.age)

class PersonDecorator(Person)

    def __init__(self, person):
        self._person = person
    def __getattr__(self, name):
        return getattr(self.__person, name)
    def __str__(self):
        age = self._person.age
        color = NORMAL
        if age >= 30:
            color =RED
        elif age >= 20:
            color=YELLOW
        return '%s%s%s' % (color, self._person.__str__(), NORMAL)

def main():
    p = []

    p.append(Person('Micheal', 25))
    p.append(Person('Bill', 2))
    p.append(Person('Ryan', 40))
    p.append(Person('Matt', 21))

    for person in p:
        if '-c' in sys.argv
        person = PersonDecorator(person)
        print person

if __name__ = '__main__'
    main()

Upvotes: 2

Views: 398

Answers (1)

aIKid
aIKid

Reputation: 28342

Don't worry, class object exists. It's a built-in type, and always there in python.

More: Built-in Functions: object()

Upvotes: 2

Related Questions