Python-Ref > Object oriented programming > Basics > Constructor
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Constructor

What happens when a new instance is created?
When a new instance of an object is created its special method names __init__ is called behind the scenes. This method is called the constructor because it is called on instance creation and is responsible for setting up all the necessary attributes of the instance. Thus the constructor is the basic mechanism that ensures consistency of attributes between different instances of the same class.
As other methods the constructor can accept arguments, both normal and keyword arguments. When present, such arguments are the required when the instance is created. On the other hand there is no requirement that all the attributes that the class supports must be reflected in the arguments or even set in the constructor. However setting at least some safe default values to all attributes in constructor is usually a good practice.
The following examples demonstrate different approaches to constructor creation.
Expand/Shrink
Zdroj: (oop9-1.py)
  1   class Person:
  2   
  3     def __init__( self, firstname, familyname, age, position="worker"):
  4       self.firstname = firstname
  5       self.familyname = familyname
  6       self.age = age
  7       self.position = position
  8   
  9     def __str__( self):
 10       return "Person: %s %s, %d years, position=%s" % (self.firstname, self.familyname, self.age, self.position)
 11   
 12   p1 = Person( "Douglas", "Adams", 49, "writer")
 13   print p1
stdout:
Person: Douglas Adams, 49 years, position=writer
Doba běhu: 150.4 ms
Expand/Shrink
Zdroj: (oop9-2.py)
  1   class Person:
  2   
  3     def __init__( self, firstname, familyname):
  4       self.firstname = firstname
  5       self.familyname = familyname
  6       self.age = 0
  7       self.position = "unknown"
  8   
  9     def __str__( self):
 10       return "Person: %s %s, %d years, position=%s" % (self.firstname, self.familyname, self.age, self.position)
 11   
 12   p1 = Person( "Douglas", "Adams")
 13   print p1
 14   p1.age = 49
 15   p1.position = "writer"
 16   print p1
stdout:
Person: Douglas Adams, 0 years, position=unknown
Person: Douglas Adams, 49 years, position=writer
Doba běhu: 30.3 ms
Expand/Shrink
Zdroj: (oop9-3.py)
  1   class Person:
  2   
  3     def __init__( self, firstname="", familyname=""):
  4       self.firstname = firstname
  5       self.familyname = familyname
  6       self.age = 0
  7       self.position = "unknown"
  8   
  9     def __str__( self):
 10       return "Person: %s %s, %d years, position=%s" % (self.firstname, self.familyname, self.age, self.position)
 11   
 12   p1 = Person( "Douglas", "Adams")
 13   print p1
 14   p1.age = 49
 15   p1.position = "writer"
 16   print p1
 17   
 18   p2 = Person()
 19   print p2
 20   p2.familyname = "Gently"
 21   print p2
stdout:
Person: Douglas Adams, 0 years, position=unknown
Person: Douglas Adams, 49 years, position=writer
Person:  , 0 years, position=unknown
Person:  Gently, 0 years, position=unknown
Doba běhu: 31.3 ms
In the example below, you can see what may happen if not all attributes are created in the constructor.
Expand/Shrink
Zdroj: (oop9-4.py)
  1   class Person:
  2   
  3     def __init__( self, firstname="", familyname=""):
  4       self.firstname = firstname
  5       self.familyname = familyname
  6   
  7     def __str__( self):
  8       return "Person: %s %s, %d years, position=%s" % (self.firstname, self.familyname, self.age, self.position)
  9   
 10   p1 = Person( "Douglas", "Adams")
 11   print p1
stderr:
Traceback (most recent call last):
  File "oop9-4.py", line 11, in ?
    print p1
  File "oop9-4.py", line 8, in __str__
    return "Person: %s %s, %d years, position=%s" % (self.firstname, self.familyname, self.age, self.position)
AttributeError: Person instance has no attribute 'age'
Doba běhu: 30.8 ms