-1

Hey guys, I am new to python development. I have studied about the classes and functions in python and creating functions arguments and all. I have seen code like this:

class person(object):
    def init(self,name):
        self.name=name
    def info(self)
        print "My name is {0}, I am a {1}".format(self.name,self.__class__.__name__)
bob = person(name='Robert')
bob.info()

This produces the output "My name is Robert, I am a person"

I just need to know the use of object in class person(object). Can we use any names instead of objects there? Why are we assigning such an object with the class name? How is it used? I have searched many times and didn't find any useful answer.

Any help would be appreciated ..Thanks

fedorSmirnov
  • 701
  • 3
  • 9
  • 19
william
  • 33
  • 5

4 Answers4

2

The parameters in brackets when initialising a class spell out what classes the defined class inherits from. You're basically saying, person inherits from object, or person is a type of object.

Python class inherits object

There are small differences between inheriting object and not.

As to what the thing in the bracket does, it's for when you want to make more complex objects, which are subclasses of other objects

For example:

class LivingThing(object):
    def __init__(self, lifespan):
        self.lifespan = lifespan

    def lifespan_info(self):
        print("this object's lifespan is {}".format(lifespan))

class Person(LivingThing):
    # inherits from LivingThing
    def __init__(self, gender):
        LivingThing.__init__(self, 80)
        self.gender = gender

    def gender_info(self):
        print("this object's gender is {}".format(gender))


person1 = Person("male")
person1.lifespan_info()
person1.gender_info()

The idea behind object-oriented design is that you can make objects which derive from other objects (in this case, "Person" is a type of "LivingThing") and if done correctly, it can make your code much easier to maintain, extend, and read.

Since Person is a subclass of LivingThing, Person can use the methods defined within LivingThing as well, and can also override its methods if needed.

Inheriting from object isn't actually a big deal - a lot of people consider it optional. It was introduced in Python2.3 and makes a "new-style object" - check here for the list of differences. What is the difference between old style and new style classes in Python?

Community
  • 1
  • 1
icedtrees
  • 6,134
  • 5
  • 25
  • 35
2

New-style classes have to inherit from the object class.

More information can be found here: http://www.python.org/doc/newstyle/

Colin Bernet
  • 1,354
  • 9
  • 12
0

I just need to know the use of object in class person(object) .Can we use any names instead of objects there.Why are we assigning such an object with the class name .Whats its use .I have searched manyt imes and didnt found any useful answer.

object in this case is the parent class of class person. You can inherit a class from other classes. This is called inheritance and is one of the core concepts of Object Oriented Programming (OOP). OOP is not just a Python thing, you can read about its concepts in other languages too.

In this particular case you inherit your class from built-in Python class called object to support some new features introduced at some point into Python.

warvariuc
  • 57,116
  • 41
  • 173
  • 227
0

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()
Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22