Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine what to put inside the __init__ function in a python class?

So I understand how to use init when defining a class in Python. However, I am confused as to what to put inside the init function, or if I should use it at all. For example:

Here I make the user define concept when the class is instantiated:

class Fruit(object):
    def __init__(self, concept):
        self.concept = concept

But I could also do this, and the user can just change the concept attribute themselves:

class Fruit(object):
    def __init__(self):
        self.concept = "I am a fruit"

Or I could just abandon the init function altogether:

class Fruit(object):
    concept = "I am a fruit"

In each of these examples, I can change the concept attribute as such

test = Fruit()
test.concept = 'Whatever I want'

So I'm not sure why I use the init function in the first place. It seems to only be convenient for defining all the attributes up front.

Moreover, how do I know what to put inside an init function and what to just define outside of the init?

like image 305
Daniel Avatar asked Jan 28 '17 17:01

Daniel


People also ask

What does __ init __ do in a class?

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.

How do you call a class method inside an init method in Python?

Use self. f() to call a class method inside of __init__ Within the method definition of __init__(self) , call self. f() where f is the name of the class method.

What is the correct syntax for defining an __ Init__ method in Python?

The __init__ method with default parameters The __init__() method's parameters can have default values. For example: class Person: def __init__(self, name, age=22): self.name = name self. age = age if __name__ == '__main__': person = Person('John') print(f"I'm {person.name}.

What is the correct syntax for defining an __ Init__ method that sets instance specific attributes?

Note that The correct syntax for defining an __init__() method that sets instance-specific attributes upon creation of a new class instance is def __init__(self): pass.


1 Answers

class Fruit(object):
    def __init__(self, concept):
        self.concept = concept

is the best approach because it allows you, as the maintainer of the class, flexibility in changing the inner workings. In addition, you may have data inside the class that should not be accessed by the client (in Python, it's not entirely possible to protect data, but there are some conventions that exist).

For example:

class Fruit(object):
    def __init__(self, concept):
        self.concept = concept
        self._super_secret_key = get_secret_key(concept)

You might not want _super_secret_key to be accessible by a client (again, it still is) but by prefixing the instance variable with _, you are conveying that it should be private.

In addition, the purpose of __init__ is to initialize the instance, so any initialization (such as setting variables) should be done there. You may want to read over Why do we use __init__ in python classes?.


class Fruit(object):
    def __init__(self):
        self.concept = "I am a fruit"

In each of these examples, I can change the concept attribute as such

test = Fruit()
test.concept = 'Whatever I want'

That type of code becomes messy very quickly. What if a year from now, you change the variable name of concept, or abandon that usage altogether? All of the clients of that class (i.e. any time you use the Fruit class) are now non-functional.


class Fruit(object):
    concept = "I am a fruit"

This serves an entirely different purpose, as concept is now a class variable, not an instance variable. As such, it is shared by all instances and can be modified in similar fashion:

In [1]: class Fruit(object):
   ...:     concept = "I am a fruit"
   ...:     

In [2]: a = Fruit()

In [3]: a.concept
Out[3]: 'I am a fruit'

In [4]: Fruit.concept = "I am NOT a fruit"

In [5]: a.concept # a.concept has changed just by changing Fruit.concept
Out[5]: 'I am NOT a fruit'
like image 118
Rushy Panchal Avatar answered Oct 21 '22 18:10

Rushy Panchal