r/learnpython • u/TurtleMcTurtl • Sep 10 '21
I'm new and I feel like __init__ is pointless, please help
I'm very new to programming, I've only been actively programming for roughly 2 weeks and the entire time, I've found no reason anyone could possibly find a good reason to use the __init__ constructer. __init__ just seems like the more complicated way to do something.
Can someone please give me a reason why I would absolutely need the __init__ constructer, or would at least find it very useful?
I think I just don't understand it's use. But I've watched so many videos on __init__ and I feel like I most definitely understand it's uses and just can't understand why someone would use it. But I see so many people using __init__, so surely it's very useful.
2
Sep 11 '21
Hey, this would have been better if you put what you would prefer to do rather than use an init. This means that we can gain a better understanding of what you think it is supposed to do.
I've found no reason anyone could possibly find a good reason to use the init constructer. init just seems like the more complicated way to do something.
Sure, you want to make a class that can represent multiple people. People have a name so you need each instance of that class to have it and you need to have it the moment that you initialise the instance.
class Person:
def __init__(self, name):
self.name = name
def describe(self):
return f'Hey, my name is {self.name.capitalize()}'
person1 = Person("peter")
person2 = Person("Paul")
print(person1.describe())
print(person2.describe())Maybe you are thinking, can't I just declare the variables and then instantiate them one by one after creation?
class Person:
name: str
def describe(self):
return f'Hey, my name is {self.name.capitalize()}'
person1 = Person()
person1.name = 'Peter'
person2 = Person()
print(person1.describe())
print(person2.describe())You can, but that means people can access them without the name.
Can I just comment on this:
I've only been actively programming for roughly 2 weeks
I personally feel that using classes/objects two weeks (or earlier) in programming is likely too early. There are a lot of fundamentals that you probably need to master before classes/objects start making a lot of sense. I am not surprised that you are missing some parts.
0
u/TurtleMcTurtl Sep 11 '21
I've been seeing it as:
can't I just declare the variables and then instantiate them one by one after creation?
If I ever need to change anything in a variable in a class I can just simply do:
class Person: name = "" name2 = ""Person.name = "Larry " Person.name2 = "John"
print(Person.name + Person.name2)
And since the only amount of use I see in __init__ is the ability to change a thing or things and set a thing or things == to something; I see my example as easier to understand and more practical. I'm aware that I lack a heap of experience and understand that I'm more than likely wrong about it being more practical though.
Hopefully that also covers:
This means that we can gain a better understanding of what you think it is supposed to do.
I don't think I understand what you mean by "you can, but that means people can access them without the name." Because something like this:
class Person: name = "John"print(name)
won't work.
Maybe it would help me understand it best if I see it used in a simple but useful program, because I'm struggling to connect these examples with actual use-case scenarios.
1
u/Binary101010 Sep 11 '21
can't I just declare the variables and then instantiate them one by one after creation?
You can, but that doesn't mean an
__init__()method isn't useful. It's just as much for the programmer as it is for the interpreter, because putting an attribute definition in__init__()tells the programmer "these are important pieces of information that every member of this class should have because other methods in this class are going to need these things set to do their jobs."0
u/TurtleMcTurtl Sep 11 '21
I see how that could be very helpful in a multi-developer scenario. Of course, right now I'm working alone, but I suppose that adds reason to why I should I learn the usefulness of init and make habit of using init for potential future usage where I might not be working alone.
2
u/wsppan Sep 11 '21
Nothing anyone has told you has anything at all to do with working in teams vs alone. Nada. You really need to take the time to truly grok Object-Oriented Programming (OOP) in Python
1
u/icecapade ML SWE Sep 11 '21
The post you're replying to had absolutely nothing to do with multiple developers, or developers/people at all. You really need to just continue learning so you can a) become familiar with the relevant terminology, and b) more importantly, understand how the concepts you're learning about are used and why they're used.
1
Sep 11 '21
I see how that could be very helpful in a multi-developer scenario.
It's actually essential to object-oriented programming. A class is a pattern for an unlimited number of objects. 1 Person, 1 name.
1
u/sickofgooglesshit Sep 17 '21
YOU are the other developer. You are ALWAYS in a 'multi developer environment' because you six months from now is not the you now and they're going to be wondering who the asshole is that wrote this code or they're going to forget that you have to do that special extra couple of lines.
And if you ever want to work professionally? You best start writing like it now so you can kick these bad habits.
OOP isn't the only style of programming, but it has some advantages. The problem with your
nameexample is that it doesn't prevent you from doing things like this:
person.name = 53Or nevermind Person, what about if you were writing a program for a bank and had an
Account? You need a balance on there and you have to make sure that it always starts with 0. Yea, same deal as the Person, except with your style, everytime you create an account, you have to remember to set it to 0. That's tedious af. If only I could have it set to 0 automatically....Account()boom done.Or what if I wanted to make a withdrawal? Without those method and the like, every single time I initiate that, I have to write all of the code that checks if they first have a balance high enough. If only I could call a single method and throw an error if they don't have enough....
def withdraw (self, amount): if amount > self.balance: print ("NO CASH FOR YOU!") return 0 else: self.balance -= amount return amountBoom. Done. I've written my code in one place, it'll always 'come with', only works on/with Account objects, and I don't have to remember where or what other things I have to do each time.
We have something called DRY in programming, it stands for Don't Repeat Yourself. If you're writing the same block of code more than once, you're probably doing something wrong.
2
Sep 11 '21
Person.name = "Larry " Person.name2 = "John"
Okay, you have 100 people. Now what? Are you going to write a class variable for every single one of them? What if you have a million people? What if you don't know how many people you are going to have?
import names class Person: def __init__(self, name): self.name = name def describe(self): return f"My name is {self.name}" people = [] for _ in range(100): person = Person(names.get_full_name()) people.append(person) for person in people: print(person.describe())I see my example as easier to understand and more practical.
Your example is misusing classes. The idea of a class is that it is a blueprint that handles the concept of a person. You use that blueprint for all of them but each object you build from that blueprint has subtle differences.
Here's an analogy:
I design a blueprint for a studio apartment. They are all N meters wide by M meters long. The blueprint is exactly the same for all 100 of them. However, once they are built they all have different variable attributes right? They have different address numbers, different carpets, different people living in them and ect. But they all have the same pattern.
That's what a class is. It's the pattern for a thing.
I don't think I understand what you mean by "you can, but that means people can access them without the name." Because something like this:
I gave you sample code:
class Person: name: str def describe(self): return f'Hey, my name is {self.name.capitalize()}' person1 = Person() person1.name = 'Peter' person2 = Person() print(person1.describe()) print(person2.describe())If you run this code it throws an unhandled exception and stops. Code isn't supposed to do that.
Here's a fairly fundamental level class/object program. It's a "park" filled with people. People play with each other and sometimes people leave the park. You'll see that the person class is 9 lines of code long and it supports 1000 people with ease.
import names import random class Person: def __init__(self, name): self.name = name def plays_with(self, person2): return f'{self.name} plays with {person2.name}' def leaves(self): return f'{self.name} leaves the park. Bye bye!' park = [] for _ in range(1000): person = Person(names.get_first_name()) park.append(person) while len(park) > 0: person1 = random.choice(park) person2 = random.choice(park) print(person1.plays_with(person2)) if random.randint(0, 2) > 1: person3 = random.choice(park) print(person3.leaves()) park.remove(person3)2
u/wsppan Sep 11 '21
You really need to slow down and understand the difference between a Class variable, and instance or object variable, and what and how OOP works.
45
u/czarrie Sep 10 '21
I'm going to be blunt, you've been at it for two weeks. You're not going to see a need for it because you haven't done enough programming to understand the problem that it solves and why it is the way that it is.
Any instance of an object basically has code that needs to happen before the rest occurs. And because different instances can do different things, you'll want to set them up differently, and init can catch stuff you throw into a new instance and help you set it up the way you want it. It isn't that it is the most amazing thing ever, but rather, you need to have a way to do this and this is how Python does it.
15
u/1544756405 Sep 10 '21
This is spot on. Being blunt is okay here.
Additionally, it is entirely possible to program for years, even professionally, and not see the point of object-oriented design. Many small projects will not benefit from an OO design.
3
u/HoratioVelvetine Sep 11 '21
While I agree, doing a bit of learning in Java boosted my Python like crazy. Understanding OOP made many things in Python much more understandable.
3
u/1544756405 Sep 11 '21
I learned both Java and C++ before Python. I still didn't see the point of OO design until I hit a project large enough that the OOP helped manage complexity. And managing complexity is what it's all about.
2
u/BRENNEJM Sep 11 '21
I feel like I’m in this same place where I’m starting to understand OOP but still not able to identify use cases in what I do. But I also spent longer than I should have using tons of lists because I didn’t see a use for dictionaries, and now I use dictionaries in almost everything I do.
I’m sure once I more fully understand/start using classes I’ll realize that I could have benefited from OOP all along.
1
u/Danelius90 Sep 11 '21
When you think about it, dicts and objects have similarities too. In terms of properties,
my_dict['name']is analogous tomy_obj.name. Then if you want to manipulate the data in an OOP way, you give the object instance methods. E.g. You callcar.accelerate()to increase its speed rather than manipulating the data directly. That's the key thing, it's data and behaviour. So if you're holding dicts of dicts or passing dicts into functions to manipulate data you should consider using objects instead1
u/czarrie Sep 20 '21
I am 33 and tried "learning" OOP as young as 14. It never clicked until I needed it in a project I was working on. The problem is that the examples always show you how but do a terrible job on the why; it's always a foregone conclusion that OOP is necessary in tutorials.
When it clicked with me was when I was messing around with imports and I suddenly realized that these black boxes that did all of these things I used were just complex versions of the same trivial examples; I can pull some object out of "Amazing Library of Wonders" and just change something, or subclass it, or whatever. It's not just a convenient way of separating different types of dogs (groan).
5
Sep 11 '21
It's like asking why people drive cars and you still haven't learned to walk. It's pointless explaining, worry more about learning what you need to learn at the moment, to get past being an absolute newbie, and you'll get the answer to your questions if you choose the right timing to ask.
4
u/carcigenicate Carcigenicate Sep 10 '21 edited Sep 10 '21
You don't really need an __init__, but it's the designated way to initialize your object. If you're creating a player object, and you want for them to have a level attribute that keeps track of their level, you might have:
class Player:
pass
def new_player():
player = Player()
player.level = 0
return playerAnd to create a new player, you'd call new_player which handles the initialization.
Because that initialization step is a common need though, a special method was created to handle initialization:
class Player:
def __init__(self):
self.level = 0
player = Player()
print(player.level) # 0This is called after the object is created to initialize the object.
3
u/LeiterHaus Sep 10 '21 edited Sep 10 '21
Does this help at all, or not really?
Edit: It's actually a good thread to follow down. A few more things are explained well.
1
u/wsppan Sep 11 '21 edited Sep 11 '21
That is a good explanation. One thing that is not quite correct:
__init__ builds your house! It initializes it. self is the reference to the house before it is built.
This is not accurate. The first sentence is not correct. You build your house prior to __init__ getting called. It is why __init__ takes self as the first parameter. Second sentence is correct. __init__ is there to be automatically called after creation. You can create your own initialization method and call it manually if you want. Third sentence is wrong. Self is the reference to the house after it has been built/created. Self is required as the first parameter of every method that can be called after you create your house.
I think the OC mixed and matched build, create and initialize making it a bit confusing as well as their definition of self being specific to init.
2
Sep 11 '21
“Sometimes you don’t think it be like it is, but it do.”
That being said, there’s a lot of stuff you won’t understand. Learn the basics, and keep an open mind. The best part of this endeavor is the satisfaction you get when you finally go “OHHHHHHHH, that’s what that is.” Cheers and don’t rush, take your time and MASTER THE BASICS.”
1
1
u/Hot_Nefariousness139 Sep 11 '21 edited Sep 11 '21
When I was learning how to make apps with tkinter it was really useful to make the entire app as a custom class that inherits from the tkinter app class, initialize everything in an init function and then create a new instance of the class. One of the main advantages of this approach is I didn't have to worry about the order in which I declared all the functions because they are already declared when the class is created. Without an OO design then you have to worry about the order of your function declaraton because you can't call a function that hasn't been declared yet. Also in general, sometimes you just want some things to be taken care of automatically when you initialize an object every single time. It keeps you from having to write repetitive code.
9
u/alphaBEE_1 Sep 10 '21
Imagine you have a cloth factory (class) and init is your machine which makes one type of clothes(having same no of pockets, colour, material). Why would you need it? 1. You don't wanna declare arguments repeatedly for multiple functions when they can be done using init . Let me take another example say humans is our class right. This clan has multiple different methods males, females, others. Now Ik that humans have some of the characteristics same irrespective of gender. So instead of having arguments like hair color, skin colour, langauge, weight for each of the functions(males, females, other)... using init I would have to write it just once and all the humans can inherit it with their own values. Also it doesn't have to be named init (it can literally be named whatever you want) but since community has been using this standard since long time it's easier for everyone to understand if you use same name. Like if I see init ik what's it for.