Is it necessary to include __init__ as the first function every time in a class in Python?

Question:

In Python, I want to know if it is necessary to include __init__ as the first method while creating a class, as in the example below:

class ExampleClass: 

    def __init__(self, some_message): 
        self.message = some_message 
        print "New Class instance created, with message:" 
        print self.message 

Also, why do we use self to call methods?
Can someone explain the use of “self” in detail?

Also, why do we use pass statement in Python?

Asked By: NOOB

||

Answers:

No, it isn’t necessary.

For example.

class A(object):
    def f():
        print 'foo'

And you can of course use it, in this manner:

a = A()
a.f()

In fact you can even define a class in this manner.

class A:
    pass

However, defining __init__ is a common practice because instances of a class usually store some sort of state information or data and the methods of the class offer a way to manipulate or do something with that state information or data. __init__ allows us to initialize this state information or data while creating an instance of the class.

Here is a complete example.

class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()

An instance of the class is always passed as the first argument to a method of the class. For example if there is class A and you have an instance a = A(), whenever you call a.foo(x, y), Python calls foo(a, x, y) of class A automatically. (Note the first argument.) By convention, we name this first argument as self.

Answered By: Susam Pal

You don’t need to put it in your Class; it is the object constructor.

You will need it if you want things to happen automatically to your object when it is instantiated.

Answered By: Lucas Kauffman

Sure that this not required.

Please read more about defining python classes here and here.

Read more about __init__ you can here and Python __init__ and self what do they do?.

In general __init__ is a kind of constructor that is called automatically and allows you to perform any additional actions(adding variables, calling any methods and so on – the idea is to have the ability to initialize instance since it is already created and now you may need to do something with it before proceeding – for example remember creation time or serializing its initial state and so on) while creating object. So if you don’t need to do some special preparation you may skip using it.

Answered By: Artsiom Rudzenka

In addition to other answers, one point in your question that has not been addressed :

Is it necessary to include __init__ as the first function
everytime in a class in Python?

The answer is no. In the case you need a constructor, it can be located at any position of your code, although the conventional and logical place is the beginning.

Answered By: joaquin

No, it is not necessary to use the init in a class. It’s a object constructor that define default values upon calling the class.

If you’re programming in OOP manner and ought to have a basic structure of your class. You often will need this.

I read your other sub-question regarding

Can u explain about the use of “self”??? – harsh Jul 28 ’11 at 5:13

Please refer to this post in stackoverflow. There’s a lot of useful links to help you better understand python’s init function.

Python __init__ and self what do they do?

Answered By: Nicholas TJ

Is not necessary the “init” statement, besides the “pass” statement is just used for skip, either to the next part of the code, or just skip cause was reached a special part like an “if” statement.

Answered By: Juan Zuluaga

I initially struggled with that question too then I realize it is just another way to store certain data to your object and that data can be passed to any object method you define since your instance method has a self argument that can point back to the data you created in the init method.

Answered By: slopeofhope

Its not necessary…
It’s just function that runs everytime an object is created from your class…
And it can be helpful if you want every object have some things in common

Answered By: Dami Hahfo

No, it is not necessary but it helps in so many ways. people from Java or OOPS background understand better.
For every class instance, there is an object chaining that needs to complete when we instantiate any class by creating an object.
If we don’t put it compiler/interpreter puts it. But when we need some action to be formed while creating an object then we must have to pass it.

first.py
-------
class A:
    def one(self):
        print("something")
second.py
----------
from first import A
class B:

    def test(self):
        a = A()
        x = a.one()
        print(x)
    test(any)
**output:**
something
None
Answered By: Akhilesh
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.