r/learnpython • u/Chaos-n-Dissonance • Dec 06 '21
Question... Why always use __init__ and self?
So I'm struggling to see the advantage of using these. I'm just starting to learn python, made a basic command prompt RPG style game... Working on moving over to tkinter to add some graphics, and everything I see when I google something people are always using __init__ and self. I kinda understand how these work, but I'm just failing to see the advantage of using it over just passing values between functions (with function(value) or just making the object global if it's being used a lot). Is this just a format thing that's become the norm or is there an actual reason?
2
u/CowboyBoats Dec 06 '21
The point of it is, let's say you're making an HR software suite and you're writing compensation logic, so you have functions concerning employee data, and 80%+ of the functions that you're writing end up using the same variables - you write repetitive code, passing to your functions the variables for base salary, exempt or non-exempt status, hours per week, bonus potential, bonus attainment, stock potential, stock attainment...
At some point someone looked at all this and said "I'm just going to code up the ability to create new data structures such as a class Employee, and define the salary, bonus, stock etc. when I initialize that data structure, and then be able to refer to it whenever I want." That became known as object-oriented programming, and Python is a popular language for OOP.
1
u/Chaos-n-Dissonance Dec 06 '21 edited Dec 06 '21
See, I like classes and used them a lot in the little game I made (player, enemy, equipment, etc)... I just don't see the value in using .self and __init__ over just passing an object between functions, since the object could hold all those things you're talking about and define base values without the use of __init__ or self.
So let's say I have something like this:
class aThing: # Notice no functions, just the class mana = 1 life = 10 name = "Player" # I didn't typo the indent, this function is separate from the class def viewStats(thing): print("Name: %s\nLife: %d\nMana: %d" % (thing.name, thing.life, thing.mana)) return def checkStats(thing): if (thing.name == "Rat"): thing.mana = 0 thing.life = 5 if (thing.name == "Spider"): thing.mana = 0 thing.life = 1 #Continue list of possible enemies and their stats return thing player = aThing() viewStats(player) enemy = aThing() enemy.name = "Rat" enemy = checkStats(enemy) print("") viewStats(enemy)Would give me the following output:
Player Life: 10 Mana: 1 Rat Life: 5 Mana: 0How and why would __init__ and self. make that better? (I'm not trying to be argumentative, legitimately want to learn)
5
u/Binary101010 Dec 06 '21 edited Dec 06 '21
So right here:
enemy = aThing() enemy.name = "Rat" enemy = checkStats(enemy)You're using three lines of code and another function call just to set the three attributes of this object. And you have to repeat this every time you create a new object. And you also have to remember to manually change the name of every new object you create before running
checkStatsto get the right results.Imagine instead if your class were:
class aThing: def __init__(self,name): self.name = name if self.name == "Rat": self.mana = 0 self.life = 5 elif self.name == "Spider": self.mana = 0 self.life = 1 elif self.name == "Player": self.mana = 1 self.life = 10Now I can just create a rat with
enemy = aThing("Rat")And the logic in that method will set everything else for me. (You could probably streamline this even further down the road pulling from a dict so you wouldn't even need the if/elif block)
Even without adding anything else, you've already turned multiple bits of code you previously had to manually execute for every object you made into code that's automatically called for you at object creation.
Now imagine we've added another method to that class:
def __str__(self): return "Name: %s\nLife: %d\nMana: %d" % (self.name, self.life, self.mana)Now we don't even have to remember another function name to print out that information, we can just do
print(enemy)Another benefit of this is that we can import this class into other code we write and all of this functionality comes along for the ride without us having to do anything extra:
from thatfirstgameiwrote import aThing1
u/Chaos-n-Dissonance Dec 06 '21
I haven't run across __str__ yet... What exactly does that do? Just set a value for if you try to call on the object as if it were a string?
1
u/Binary101010 Dec 06 '21
Yes, it's a "magic method" for classes that defines the "user-friendly" string representation of an object.
https://docs.python.org/3/reference/datamodel.html#object.str
1
u/synthphreak Dec 06 '21
When you call
stron something,__str__gets called behind the scenes.>>> class C1: ... def __str__(self): ... return 'foo' ... >>> c1 = C1() >>> c1 <__main__.C1 at 0x7f80c3759490> >>> str(c1) 'foo'Compare this to a class where
__str__is not explicitly defined.>>> class C2: ... pass ... >>> c2 = C2() >>> c2 <__main__.C2 at 0x7f80c41500d0> >>> str(c2) '<__main__.C2 object at 0x7f80c41500d0>'Defining
__str__is useful in several cases beyond just the obvious one of directly callingstr. For example, it's useful for logging, and also for f-strings.>>> f"My C1 class' str method goes {c1}" "My C1 class' str method goes foo"
2
Dec 06 '21
I kinda understand how these work, but I'm just failing to see the advantage of using it over just passing values between functions
The purpose of __init__ isn't to pass values around. It's to initialize the object so that it's in the intended, initial state. That's why the name of the method is "init", it's short for "initialize".
1
u/Chaos-n-Dissonance Dec 06 '21
Yeah I guess that's more towards creating functions within a class... Like for example I could have:
import math class Player(): strength = 1 life = 10 experience = 0 level = 1 def levelUp(a): a.strength = int(math.floor(a.strength * 1.1)) a.life = int(math.floor(a.life* 1.1)) a.experience = 0 a.level += 1 return a def displayStats(a): print("Strength: %d\nLife: %d\nExperience: %d\nLevel: %d\n" % (a.strength, a.life, a.experience, a.level)) return a = Player() displayStats(a) levelUp(a) print("") displayStats(a)Or...
import math class Player(): def __init__(self): self.strength = 1 self.life = 10 self.experience = 0 self.level = 1 def levelUp(self): self.strength = int(math.floor(self.strength * 1.1)) self.life = int(math.floor(self.life * 1.1)) self.experience = 0 self.level += 1 def displayStats(self): print("Strength: %d\nLife: %d\nExperience: %d\nLevel: %d\n" % (self.strength, self.life, self.experience, self.level)) a = Player() a.displayStats() a.levelUp() a.displayStats()And in both cases the output is the same:
Strength: 1 Life: 10 Experience: 0 Level: 1 Strength: 1 Life: 11 Experience: 0 Level: 2So what's the advantage to putting all of the functions within the class rather than being housed separately and passing the object between functions?
2
Dec 06 '21
So what's the advantage to putting all of the functions within the class rather than being housed separately and passing the object between functions?
Encapsulation is the advantage. The functions being members of the class mean you can access them through the object and its namespace, rather than having to pluck them out of some module's namespace. In your first example, imagine that this is all in a module and you're trying to access these functions from a different module. How do you get a reference to
displayStatsin order to call it on the object? Well, now you have to know something about the module it's contained in, what it's called (so you can import it to get access to its namespace), and what else it might have in it that you want to use. And what happens when you calldisplayStatson something that isn't aPlayer? How do you know not to do that?On the other hand, when
displayStatsis a member of thePlayerclass, all you have to know aboutais its type (that it's an object of typePlayer) and now you have some knowledge about what methods are available to you. And if you don't, then you can justprint(dir(a))and see what it's got. You don't need to import anything if you're passed the value ofa, as its encapsulated and self-contained.1
u/javaHoosier Dec 06 '21 edited Dec 06 '21
If are asking what the point of “self” itself is and functions on the class?
In that case what your describing exists. Its called functional programming using structs (outside of python and out of the scope of this) and its very popular. They are just different ways for us to represent an intuitive understanding of modeling code.
As for the term self itself. A lot of languages allow you to omit it and have it be implicit if it annoys you.
58
u/velocibadgery Dec 06 '21 edited Dec 06 '21
On small projects you might not see the advantage, but it really comes into play in larger projects.
Classes allow you to reuse code really easily. And it really comes into play with data management.
For example, if I wanted to represent a book, I could use something like a dictionary
book1 = {"name": "The name of the wind", "author", "Patrick Rothfuss"}now this seems simple. But if I do this,
class Book
def __init__(self, name, author):
self.name = name
self.author = authorNow I can create a book just like this
book1 = Book("The name of the wind", "Patrick Rothfuss")this is just simpler. and I can build in functionality that a dictionary just doesn't have. Like a nice way to print everything.
class Book:
def __init__(self, name, author):
self.name = name
self.author = author
def __str__(self):
return f"Name: {self.name}\nAuthor: {self.author}And then I can print that book really easily
print(book1)and with that one line I get the following output
Name: The Name of the Wind
Author: Patrick Rothfuss
And the real great thing about classes is I can put that book class in a separate file and reuse it anytime I need to represent a book in my code.
from my_classes import Bookand now I don't have to write out that code again in my next project. I can just do
book1 = Book("Mistborn", "Brandon Sanderson")and everything just works.
I can also do things like add additional code onto a class
class NewBook(Book):
class __init__(self, name, author, isbn):
super().__init__(name, author)
self.isbn = isbnand I can use that new class just like the other one
book1 = NewBook("Fables of Aesop", "Aesop", "10290301723")and without rewriting the nice code to print everything, I can still do
print(book1)and get the output
Name: Fables of Aesop
Author: Aesop
because it is extending the base class Book, I get to keep all that code. but my new code also works
print(book1.isbn)and the output
10290301723
So classes just make your life simpler all around. Especially when managing large amounts of data or large amounts of code that you reuse all the time.
Edit: Minor correction, I mistakenly typed Title instead of Name in my output.
9
u/Zeerats Dec 06 '21
That's one complete answer. Great refresher for me as a beginner too. Thanks!
8
u/velocibadgery Dec 06 '21
yw :). I am primarily a c# programmer, and I just love classes. I try and make everything I do into a class if possible. So it was one of the first things I learned in Python. I am really loving the python language, it is just super.
5
3
2
u/QuickSketchKC Dec 06 '21
Thanks for comprehensive post, what i fail to understand is why not use class name instead of "self". Is it because of ease of instantiation and nothing more?
4
u/velocibadgery Dec 06 '21
When you use the class name you are accessing class variables which are different than instance variables.
BTW, self is just a naming convention for the instance, you can really call it whatever you want.
def __init__(bob, name, author): bob.name = name bob.author = author.We use self just as a standard. But the first argument passed in a class method is always the instance reference.
If instead you assign like this
class Book def __init__(self, name): Book.name = nameThen that value is the same across all instances. It is like a global for the class.
2
2
2
u/y3snomaybe Novice Mar 26 '22
Hello, I liked your detailed and easy explanation. I understood it clearly. Can you explain the __init__ method, similarly? Why do we use it?
2
u/velocibadgery Mar 26 '22
Init is the constructor. It is called when you create a new object.
You use it to set up an object.
So with our book class, I could make one without the init.
class book: name: str author: strAnd this is a valid class, but then you have to create it like this
a = book() a.name = 'mistborn' a.author = 'brandon Sanderson'The constructor is just a function that gets automatically called when the object is created. You can use it to pass information into your class variables.
Did this help?
2
u/y3snomaybe Novice Mar 30 '22
Yes, it did help. Thank you. I am learning to use Python for data analysis. My goal is to be a data analyst. And I read that, most of the data analyst workflow involves procedural programming. So, not going too deep into OOP. But, I did understand the basics of OOP( class, instance, self, __init__, methods and attributes).
7
Dec 06 '21
The __init__() method of a class is used to initialize each instance of the class. You don't actually need to write an __init__() method and in that case you get instances that are exactly the same as all the others, and you have to write other methods (or use attributes) to change values that are different in different instances. Suppose you had a Person class and you wanted different people to have different names and ages. Using an __init__() method you could do this:
person1 = Person('Harriet', 26) # using the __init__() method
person2 = Person('Tom', 29)If you didn't write an __init__() method you would have to do this:
person1 = Person()
person1.name = 'Harriet'
person1.age = 26
person2 = Person('Tom', 29)
person2.name = 'Tom'
person2.age = 29Which would you rather do? In addition, in that second example the person using the class would have to know what the attributes for name and age were called. In the __init__() case all that is hidden and the user doesn't care what the attribute names are.
Not sure what your problem with self is. If you are asking what self is used for then the simple answer is that self is the reference to the instance the method is to work on. When you write a class method the code works on an instance, but there can be hundreds or millions of instances of a class. The code has to know which particular instance it is working on and the self reference tells it.
If you mean why use the name self for the instance reference the answer is that it's just the conventional name we use. You can use any name you like, though not using self would be considered odd.
2
u/IAmASquidInSpace from solutions import * Dec 06 '21
Isn't it generally considered bad practice to assign attributes to an object outside of it's constructor method? Or is it just PyCharm being unhappy when I do it? By assign I mean defining a new attribute, not assigning a new value to an existing attribute. The latter would make sense.
3
Dec 06 '21
I don't use pycharm, so I don't know why it's "unhappy".
If you mean creating new attributes outside the class methods then it's bad practice, since the user of the class has to know the actual attribute name which is bad OOP practice since we try not to advertise the internals of a class as much as possible. Plus, the user might get the name wrong and the code crashes some time later because the attribute wasn't set.
If you mean you create an attribute in a method that isn't
__init__()then that is also bad practice. Doing that risks getting an "undefined" exception when you call a method that uses the attribute but your code hadn't yet called the other method that actually creates the attribute. Again, the user has to know too much.1
u/IAmASquidInSpace from solutions import * Dec 06 '21
Thanks! I think in this case PyCharm is actually unhappy to enforce the ideas you have described, i.e. to stop me from running into both these problems.
1
u/synthphreak Dec 06 '21
Would another use case for an
__init__-less class be one whose sole purpose is to be inherited?As a trivial example, say you wanted to code up a zoo. Zoos have all kinds of things, but the vast majority of what zoos are about is animals. So you might start by creating a super high-level, atomic
Animalclass. This class defines the most basic traits and behaviors shared by all animals. Then you could create more specific classes likeElephant,Snake, etc. which all inheritAnimalas the parent.In this case, where you never directly instantiate
animal = Animal(), but rather only the child classes, wouldn't that be a clear case where__init__is totally unnecessary?Are there any other cut-and-dry use cases beyond that? I pretty much always add
__init__to my classes, but it's probably not always necessary.1
Dec 07 '21
In your example you say:
So you might start by creating a super high-level, atomic Animal class. This class defines the most basic traits and behaviors shared by all animals.
So the
__init__()method would create those traits for eachAnimalinstance, even though you say nobody would ever create anAnimalinstance. That's so classes that inherit fromAnimalcan reuse theAnimal__init__()method to create the basic traits which the subclass then adds to. If a child class defines an__init__()method, that overides the parent__init__(), but the child code can use thesuper()function to call the parent__init__()to set up the basicAnimaltraits. Doing it that way is better than repeatedly redefining the basicAnimaltraits in every child class deriving fromAnimal. Plus, when you change the traits (attributes) of theAnimalclass you don't have to change the attributes in every child class. They just call the parent__init__()method and the changed attributes are automatically created. Then the child class creates its own specific attributes.I've never written a class that didn't have an
__init__()method because there's always something that's different between instances of a class, and the__init__()method is where you set those attributes. Having many instances of a class with nothing to distinguish between them doesn't sound useful at all.
1
u/luersuve Dec 06 '21
I’ll add this video to the conversation. It explains when a class shouldn’t be a class.
1
u/bfyvfftujijg Dec 06 '21 edited Dec 12 '21
sed to everywhere but beside you. God governs only what happens while it happens: this want is wine of your own making. Loud the quieter times, and quiet has dispersed to everywhere but beside you. God governs only what happens while it happens: this want is wine of your own making. Loud the quieter times, and quiet Wist is wetness and why, wind, why. Go and gather quickly before every shadow has dispersed to everywhere
8
u/mopslik Dec 06 '21
Well, that's one of the major advantages in that you don't need to pass 20 values back and forth between your functions all the time. Everything is bundled neatly into each instance of a class.
Another major advantage if you are using classes is that it is easy to modify a class to extend another one (inheritance), or to piece together multiple classes (composition), whereas this would likely require some not-so-beautiful Frankenfunctions in a non-OOP setting.