Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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?

like image 593
NOOB Avatar asked Jul 28 '11 04:07

NOOB


People also ask

Why do we use __ Init__ function in python class?

The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach. The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.

Is __ init __ called automatically?

Use the __init__() method to initialize the object's attributes. The __init__() doesn't create an object but is automatically called after the object is created.

Why do we need init function?

The __init__ function is called a constructor, or initializer, and is automatically called when you create a new instance of a class. Within that function, the newly created object is assigned to the parameter self . The notation self. legs is an attribute called legs of the object in the variable self .

Does every class need a constructor Python?

This method is defined in the class and can be used to initialize basic variables. If you create four objects, the class constructor is called four times. Every class has a constructor, but its not required to explicitly define it.

Is it necessary to include __init__ as the first function Everytime?

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. You don't need to put it in your Class, it is the object constructor.

Which function is used to initialize a class in Python?

Python Class Constructor – Python __init__ () Function Python class constructor function job is to initialize the instance of the class. Python __init__ () is the constructor function for the classes in Python.

What is the use of init () in Python?

It binds the instance to the init () method. It’s usually named “self” to follow the naming convention. You can read more about it at Python self variable. The init () method arguments are optional. We can define a constructor with any number of arguments.

Which class constructor is called first in Python?

So, the parent class constructor is called first. But in Python, it is not compulsory that parent class constructor will always be called first. The order in which the __init__ method is called for a parent or a child class can be modified.


2 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.

like image 113
Susam Pal Avatar answered Oct 04 '22 00:10

Susam Pal


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.

like image 41
joaquin Avatar answered Oct 03 '22 23:10

joaquin