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()
3
  • 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.) Commented Oct 10, 2013 at 1:05
  • Specifically, see docs.python.org/2/reference/… . Commented Oct 10, 2013 at 1:12
  • Please read this, [stackoverflow.com/questions/4015417/… Commented Dec 6, 2013 at 20:54

1 Answer 1

2

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

More: Built-in Functions: object()

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.