2

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()
aIKid
  • 26,968
  • 4
  • 39
  • 65
david
  • 6,303
  • 16
  • 54
  • 91
  • There is a class named `object`, it is the base class of all classes. (In Python 2 it is the base class of all "new-style" classes, but that essentially means all because those are the only ones you would want to use.) – BrenBarn Oct 10 '13 at 01:05
  • Specifically, see http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes . – Peter DeGlopper Oct 10 '13 at 01:12
  • Please read this, [http://stackoverflow.com/questions/4015417/python-class-inherits-object?rq=1] – NullException Dec 06 '13 at 20:54

1 Answers1

2

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

More: Built-in Functions: object()

aIKid
  • 26,968
  • 4
  • 39
  • 65