r/learnpython • • Jul 10 '22

__init__ method

can some explain how does the __innit__ method works?

P.S: pls try to make the explanation as simple as possible.

15 Upvotes

22 comments sorted by

View all comments

20

u/Green-Sympathy-4177 Jul 11 '22

__init__ is used in Objects to initialize them

Say you have a Person object with a firstname and a lastname:

# Create a "Person" class
class Person:

    # When you create a new instance, this will be called
    def __init__(self, firstname, lastname):

        # assign the firstname passed to this instance's firstname
        self.firstname = firstname

        # assign the lastname passed to this instance's lastname
        self.lastname = lastname

        # Just print stuff to see that it's being called
        print(f"{self.firstname} {self.lastname} was initialized !")
    
# Create an instance of Person
john = Person(firstname="John", lastname="Doe")
>> John Doe was initialized!

print(john.lastname)
>> Doe

1

u/fakemoose Jul 11 '22

Why would you do it this way instead of defining a function? That’s where I always get confused with init. It seems like extra work for the same result.

2

u/Green-Sympathy-4177 Jul 11 '22

Basically __init__ is a method inside an Object, nowhere else.

In the example I provided the object is as basic as it gets.

But what if we change the class and give to "Person" object the ability to greet themselves? And a property to get the fullname from the firstname and lastname ?

class Person:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    def greet(self):
        print(f"Hi, I'm {self.fullname} !")

    @property
    def fullname(self):
        return f"{self.firstname} {self.lastname}"


alice = Person("Alice", "Pink")
bob = Person("Bob", "Red")
charlie = Person("Charlie", "Brown")

alice.greet()
>> Hi I'm Alice Pink !

And now imagine you have tons of instances. Go and manage that with functions... It's doable, but it will be a mess.

There's a lot more to the whole "object vs function", google: "OOP vs function, python"

1

u/Tiktoor Jul 11 '22

What is self? It gets assigned to the related variable name?

4

u/Green-Sympathy-4177 Jul 11 '22

self is a reference to the instance of the class, to take back the same example as before:

class Person:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    def greet(self):
        print(f"Hi, I'm {self.fullname} !")

     @property
    def fullname(self):
        return f"{self.firstname} {self.lastname}"

We have our class Person, when we instantiate this class, like this:

alice = Person("Alice", "Pink")

When we use alice.greet(), it calls self.fullname, self in that instance is the Person alice. If we had another one bob = Person("Bob", "Black") then when we're using bob, then bob is self in that case.

It's a terrible way to explain it.

Google's response:

The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.

Read more about Object Oriented Programming (OOP) in Python: