TAGS :Viewed: 5 - Published at: a few seconds ago

[ Python Class __init__ Confirmation ]

Im trying to understand Python Classes. I am a little confused around defining the __init__. If I have 4 functions created all taking various input variables. Do I have to assign each variable in the __init__?

 class Thing:
     def __init__(self, arguments, name, address, phone_number, other):
         self.arguments = arguments
         self.name = name
         self.address = address
         self.phone_number = phone_number
         self.other = other

     def First(self, name):
         print self.name

     def Arguments(self, arguments):
         print self.arguments

     def Address(self, address, phone_number):
         print self.address + str(self.phone_number)

     def Other(self, other):
         print self.other

The above is completely made up so please excuse its pointlessness (arguably its point is to illustrate my question so guess its not pointless).

No doubt im going to get loads of down marks for this question for some reason but I have been reading various books (Learning Python The Hard Way, Python For Beginners) and been reading various tutorials online but none of them actually confirm "You must add every variable in the init function". So any help understanding the __init__ a little better would be appreciated.

Answer 1

Firstly: The __init__() function is special in that it is called for you while creating a new instance of a class. Apart from that, it is a function like any other function, you can even call it manually if you want.

Then: You are free to do what you want with the parameters passed to a function. Often, the parameters passed to __init__() are stored inside the object (self) that is being initialized. Sometimes, they are used in other ways and the result then stored in self, like passing a hostname and using that to create a socket - you then only store the socket. You don't have to do anything with the parameters though, you can ignore them like you do in your First() function, which receives a parameter name which is ignored. Note that this parameter name and the similar attribute self.name are different things!


  • It is uncommon to ignore parameters passed to __init__() though, just as it is uncommon to ignore parameters in general. In some cases, they are "reserved" so they can be used by derived classes but typically they are unintended (as with your First() function, I guess).
  • Check out PEP8, which is a style guide. Adhering to it will make it easier for others to read your code. E.g. First() should be first() instead.
  • Upgrade to Python 3. I don't think there's any excuse nowadays to learn Python 2 and littley excuse except maintenance to use it in general.

Answer 2

If the variable is logically connected to the object itself, it's good to set it in constructor:

class Student:

    def __init__(self, name):
        self.name = name

    def print_name(self):

If the variable is just a temporary parameter to some function, then, well, just pass it just as a parameter:

class Cafeteria:

    def __init__(self):
        pass # nothing

    def process(self, student_name):
        print(student_name + " got lunch")

In Python you don't have to declare all possible object attributes like you have to do in C++, C#, Java etc. I think it's still good idea to initialize them in constructor to some null value (None, 0), but it's not necessary. This is just fine:

class Cafeteria:

    def set_today_menu(self, menu):
        self.menu = menu

    def process(self, student_name):
        print(student_name + " got " + self.menu)

Answer 3

You don't have to, but to access them they need to set somewhere...you can also set defaults outside of any of the defs in a class if you want.

def Class:
    RandomVariable = 5
    def __init__(self, val):
        self.RandomVariable = val

Answer 4

This method is known as your initializer where you insatiate an object and then add attributes to it. A human object, if we were to create a class, may contain a name, age, and gender attributes all of which are bound to the object using the self pointer in the initializer. Also, an initializer may modify global variables as well. If you wanted to count the number of babies born, you would add to a global counter variable from our fictitious human class initializer.