Python constructors and __init__

Why are constructors really called "constructors"? What is their purpose and how do they differ from the methods in the class?

Also, can there be more than one __init__ in a class? I tried something like the following: can someone explain the result?

 >>> class test: def __init__(self): print "init 1" def __init__(self): print "init 2" >>> s=test() init 2 

Finally, Is __init__ operator-loader?

+92
python
Jan 24 '12 at 11:11
source share
5 answers

There is no function overload in Python, which means you cannot have multiple functions with the same name but with different arguments.

In your code example, you are not overloading __init__() . It happens that the second definition associates the name __init__ with the new method, making the first method unavailable.

Regarding your general question about designers, Wikipedia is a good starting point. For Python-specific things, I highly recommend Python docs .

+105
Jan 24 '12 at 11:13
source share

Why are constructors really called "constructors"?

The constructor (named __new__ ) creates and returns a new instance of the class. So the class method C.__new__ is a constructor for class C.

C.__init__ instance C.__init__ is called for a specific instance after it is created to initialize it before passing it to the caller. So this method is the initializer for new instances of C.

How do they differ from the methods in the class?

As stated in the official documentation, __init__ is called after the instance is created. Other methods do not receive this treatment.

What is their purpose?

The purpose of the C.__new__ is to define user behavior when creating a new C instance

The purpose of the C.__init__ is to define the user initialization of each C instance after it is created.

For example, Python allows you to do:

 class Test(object): pass t = Test() tx = 10 # here you're building your object t print tx 

But if you want each Test instance to have an x attribute of 10, you can put this code in __init__ :

 class Test(object): def __init__(self): self.x = 10 t = Test() print tx 

Each instance method (the method invoked for a particular instance of the class) receives the instance as the first argument. This argument is conditionally called self

Class methods, such as the __new__ constructor, instead receive the class as the first argument.

Now, if you need custom values ​​for the x attribute, all you have to do is pass that value as the __init__ argument:

 class Test(object): def __init__(self, x): self.x = x t = Test(10) print tx z = Test(20) print tx 

I hope this helps you resolve some doubts, and since you have already received good answers to other questions, I will stop here :)

+59
Jan 24 2018-12-12T00:
source share

Classes are just drawings for creating objects. A constructor is code that runs every time an object is created. Therefore, it makes no sense to have two constructors. What happens is that the second writes the first.

Variables for this object are usually used for them:

 >>> class testing: ... def __init__(self, init_value): ... self.some_value = init_value 

So what you can do is create an object from this class as follows:

 >>> testobject = testing(5) 

Then the test object will have an object named some_value , which in this example will be 5.

 >>> testobject.some_value 5 

But you do not need to set a value for each object, as in my example. You can also do the following:

 >>> class testing: ... def __init__(self): ... self.some_value = 5 

then the value of some_value will be 5, and you will not need to set it when creating the object.

 >>> testobject = testing() >>> testobject.some_value 5 

β†’> and ... in my example, this is not what you write. What it would look like in pyshell ...

+9
Jan 24 '12 at 11:17
source share
Constructors

called automatically when creating a new object, thereby "constructing" the object. The reason you can have more than one init is because the names are just python links, and you are allowed to change what each variable refers to whenever you want (hence dynamic typing)

 def func(): #now func refers to an empty funcion pass ... func=5 #now func refers to the number 5 def func(): print "something" #now func refers to a different function 

in the definition of your class, it just saves the later

+1
Jan 24 '12 at 11:17
source share

In Python, there is no concept of method overloading. But you can achieve a similar effect by specifying optional arguments and keywords

0
Jan 24 2018-12-12T00:
source share



All Articles