Today also I wanted to refresh a bit the concept of classes etc…
Classes, objects and magic methods
What are those things like __init__ and other things like __x__? They are “Magic Methods” and they are too complex for now. In the future I can study them here.
When you start a class o initialize it, whatever attributes you put inside parenthesis, you WILL have to pass those attributes when you want to create a related object. All of them. If you miss one, you will get an error.
the def __init__ (self, name, age): basically says what happens whenever an object is created on that class. And as spoken, it demands x1,x2 whenever you create an object, otherwise it wont work. Self is just the name of the object, so “itself”.
so for a class Student, I can create and object like this student1 = Student(“Toni”,39).
the def __str__ (self): magic method basically tells the program what to do whenever an object is printed. So if I do print(student1), and I don’t have a str function, the program will return the address in memory where that object is stored. However if you have the str magic method on your class… then when doing print(student1), the program will do whatever you have told it to do under the method __str__
the def __del__(self): magic method, basically tells the program what to do whenever an object is deleted. You delete an object by saying del(). For example you can do del(student1) and the program will do whatever you want the program to do for that object in it’s class __del__ magic function.
Then I also learned that you can have variables and functions of the class, that don’t really depend on object. So you put thes ones before the def’s. Below for example there is a variable “population” that is independent on the objects.
At the same time, you can for example say that, whenever an object is created (__init__) , a certain class variable (even for another class) gets affected. Below, whenever a student object is created, the population goes 1 number higher.
Below you can also see that when I make a student object, it demands me to pass “name” argument. It is fine, but then you need to make sure that you “save” that. This is why below you can see that in the init method, whenever an object is created, self.name = name. This means that Object.name = (whatever “name” you passed).
class Student: population = 3 def __init__ (self,name): self.name = name self.attend = 0 self.grades = [] print "Hi! My name is {0} and i have attend¨{1} ".format(self.name, self.attend) Student.population += 1 def __str__(self): return "Hello" def __del__(self): print "You have deleted " + self.name
Curly Brackets to substitute
Indirectly also learned that curly brackets can be used to substitute certain variables inside of a string to avoid having to concatenate each variable.
I learned that this is actually a pretty good thing, because if you concatenate things with “+” you have to worry about str not concatenating with numbers etc…
In the case below, the first wouldn’t work because in the program we wrote beffore, attend is a number, and name is a string. The second one will work.
print “Hi! My name is ” + self.name + self.attend
print “Hi! My name is {0} and i have attend¨{1} “.format(self.name, self.attend)
I am not fully sure how the substitution works with the {}, but it seems that you basically just pass as many variables as you want at the end of the string with .format(x1.x2.x3.x4) etc… and then you can replace by those on the string by using {0}{1}{2}{3} basically.
Nice!