While the response from Colin Bernet hits the mark accurately, I would like to elaborate it with an example and some background explanation.
For a quick understanding of what the differences between old-style and new-style classes are, refer to this SO question.
That said, one of the key take-aways from the definition of new-style classes is that Python strives to achieve unification of built-in types and user-defined types. Understanding this aspect is important, since this gives you the power to extend the functionalities exposed by built-in classes with your own using type inheritance.
In your example, the class person
inherits from the built-in object
class. The parent class must be something that makes sense for your business domain.
For e.g. consider a case where you would want to implement a simple in-memory database of Person objects and you would want to return Person object given his/her last name. You could then implement it elegantly as below. Note how the provision of new style classes allows you to map and extend built-in classes to your real-world problem domain,
import os
class Person(object):
def __init__(self, firstname=None, lastname=None, age = 0):
self.__firstname = firstname
self.__lastname = lastname
self.__age = age
def get_firstname(self):
return self.__firstname
def get_lastname(self):
return self.__lastname
def get_age(self):
return self.__age
''' Create a PersonDatabase class that inherits the built-in dict type. This is an example of mapping a built-in class to the problem domain of maintaining a repository of Persons for an application'''
class PersonDatabase(dict):
def __init__(self):
pass
def add_person(self,person_object):
self[person_object.get_lastname()] = person_object
def get_person_by_lastname(self, lastname):
return self[lastname] if lastname in self else None
def main():
p1 = Person('John', 'Doe', 21)
p2 = Person('Alice', 'Jane', 25)
person_db = PersonDatabase()
person_db.add_person(p1)
person_db.add_person(p2)
#get the record from the datbase given the last name
p1 = person_db.get_person_by_lastname('Jane')
if p1 is not None:
print ' Name : ' + p1.get_firstname() + ' Last Name : ' + p1.get_lastname() + ' Age : ' + str(p1.get_age())
if __name__ == '__main__':
main()