2

I am still learning the concepts of OOP so please forgive me if this is a poor question:

If __init__ is needed to instantiate an object in Python, why does the following work with no call to __init__?:

class Person:
    #Note the lack of __init__(self):
    def run(self):
        print('I am a person')

man = Person()
man.run() #Prints 'I am a person')

3 Answers 3

3

From python documentation:

The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named init(), like this...

So __init__ isn't needed, it is the mechanism python provides to allow you to set an initial state of your object when it is initialized.

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

4 Comments

Thank you. I don't know how I missed this. As an aside, did you read the entire tutorial, and if so would you recommend it (over books on Python such as the one I am reading now)
@Startec I personally think reading the entire tutorial can be a little bit overwhelming (I didn't read it myself) but I do recommend you to use it as a reference book to look for concrete parts of the language you are interested in or you need to use: Concurrent programming, data structures, ... You will find yourself having read it mostly completely after having worked for a long time with python. This is just my opinion however.
Thanks, I have just never gotten anyone's opinion on it and was / am considering reading it straight.
@Startec Building Skills in Python, by Steven F.Lott is a must for me. I already have read it, and now I'm going to read the second title Building Skills in Object-Oriented Design
1

There is no need to create an __init__ function if you do not need to do any initialization other than that provided by the parent class. Note that __init__ functions of any parent class do get called automatically:

In [1]: class Parent(object):
   ...:     def __init__(self):
   ...:         print 'Inside __init__ of parent'

In [2]: class Person(Parent):
   ...:     def run(self):
   ...:         print 'I am a person'

In [3]: p = Person()
Inside __init__ of parent

In [4]: p.run()
I am a person

In modern Python, any class implicitly derives from the object class, which supposedly takes care of basic things like allocating memory space, even if you do not add any other properties.

Comments

0

__init__ is a Constructor, this is simple put a function that is called when you create an object. It is normally used to set up the object so that it is ready to be used. If no __init__ exist the object will still be created. It simply means that the object doesn't get anything done when constructed.

2 Comments

Technically speaking, Python uses a 2 stages initialisation scheme so there's no proper "constructor" - there are an allocator (__new__) and an initialiser (__init__). The initialiser will only get called if it exists, so it is possible to have the allocator doing all the initialisation stuff and no initialiser at all (FWIW that's the case for immutables types IIRC).
@brunodesthuilliers still, the python data reference calls the __init__ function a constructor (docs.python.org/3/reference/…). All OOP languages I know of allows the constructor to "touch" the internal members of the object, meaning that the allocation of the object has already been completed. I do not think this is a special case with python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.