0

I am fairly new to learning programming and starting with Python. I have read on the utility of using init() function in classes and understand it somewhat. However, I am still a little confused and hope someone could clarify that.

So I have two set of preliminary codes which give the same output, but one uses init(). The first one is as follows:

class person:
    def who(self, name):
        print('I am', name)

name=str('Joe')
person().who(name)

The second one is here:

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

    def who(self):
        print('I am', self.name)

p = person('Joe')
p.who()

My basic question is what would I miss in general case of my code (may be more complex at some stage) if I take the first approach and not the second one Or what I would I gain with the second approach.

Thanks!

PS: My question is not what init() means (although that is related and certainly of interest to me), but why making use of init() essentially makes the code better.

Yogesh
  • 25
  • 5
  • 1
    You don't need `p.who()` for the second approach. Just use `p.name`; there is no need to create the `who()` method at all. – roganjosh Apr 19 '17 at 16:37
  • 4
    Possible duplicate of [Why do we use \_\_init\_\_ in python classes?](http://stackoverflow.com/questions/8609153/why-do-we-use-init-in-python-classes) – Isaac Osiemo Apr 19 '17 at 16:37
  • the `__init__` method is a _class constructor_. Its used when you need to initialize your class with certain variables. But as @roganjosh has already mentioned, why do you have the `who()` method at all? The easiest way to create the class in your example would be to remove the who method all together, and simply do `print(p.name)`. – Christian Dean Apr 19 '17 at 16:41
  • Have you seen this? http://stackoverflow.com/questions/8609153/why-do-we-use-init-in-python-classes – Mohammad Ashraful Islam Apr 19 '17 at 16:44
  • But `print(p.name)` doesn't print `"I am " + p.name` :) I suspect the `who` method is just a contrived example @ChristianDean – David Zemens Apr 19 '17 at 16:44
  • 1
    @DavidZemens Well yes, I guess your correct :P So perhaps a `__repr__()` would be a better option then. – Christian Dean Apr 19 '17 at 16:47
  • 2
    @DavidZemens I think this is a case that contrived examples obscure actual functionality. I realised my own error after I posted the comment. That said, I don't think there's a good way to bridge from tutorial to actual use :( The middle-ground is hard to find. – roganjosh Apr 19 '17 at 16:50
  • Thanks! You guys are right and removing the who() function in second works. I actually read the thread suggested above. And it is really helpful in making things clearer. However once I remove the who() function, it seems the two code are pretty similar, which makes me think why use __init__() at all? – Yogesh Apr 19 '17 at 16:50
  • @Yogesh then re-read the other link. "who" is an instance property, not a class property. Or at least it *should* be. – David Zemens Apr 19 '17 at 16:51

1 Answers1

3

I believe you are missing the main purpose of classes: to provide a cohesive and modular group of data and functionality.

You created a class person, so which information does a person hold? Her name, birthday, and gender, for example.

This is what __init__() is for, it is a built-in method used to initialize a person after being born (i.e. when being instantiated). For example, after creating a person you HAVE to give it a name, a birthday and a gender. Hence, __init__() enforces that persons are always initialized in a valid state. This means that you can go to the street and ask any person you encounter for her name, while being sure that everyone has a one.

As for your second example, what makes more sense, to ask for an unknown's person name, or to tell her what her name should be?

Pablo Arias
  • 360
  • 1
  • 9
  • Is this your own analogy or part of a broader tutorial that can be linked to for future questions? – roganjosh Apr 19 '17 at 16:59
  • Thanks @Pablo! That is definitely helpful. Am I right in saying that __init__() adds to better (more efficient?) functionality of the code in the sense it initializes all the attributes (hope that is the right terminology?) like name, gender, birthday and so on. We can conveniently call upon them later in the code. Where as in the first example I will have to create a separate function for name, gender, birthday and so on. I apologize in advance if I am dumbing things down here. – Yogesh Apr 19 '17 at 17:23