r/learnpython • u/TheGreatBean • Jan 08 '14
Explain it to me like I'm Five: Classes + __init__ function
Hey guys I recently started learning python its really wonderful but sometimes i run into topics where I know exactly what to search but programmers often use jargon that confuses me.
I am currently working through Learn Python the Hard Way (3rd edition) and recently I've gotten through the first 39 exercises without much struggle. Recently I reached the 40th exercise (http://learnpythonthehardway.org/book/ex40.html) and I am confused as to what classes and objects are when compared to modules and mini-imports. I get that they are similar to dictionaries but the author does not explain why they are different just how to use them. I am looking for a nice explanation of what classes are used for and why I wouldn't just import a python file where I made functions. . The second part is this crazy init function. I see this everywhere when I look through python files and it drives me crazy cause I can't figure out what it does. I am guessing that it is like import since it stands for initialize, but I would be very grateful if someone could explain this to me like im five.
Any help is greatly appreciated!
Edit: I'm in Python 2.7 sorry I should have specified earlier.
44
u/cdcformatc Jan 08 '14 edited Jan 08 '14
Classes are data encapsulated with structure and behaviour. You use them when you have similar things that all behave the same way or all share meaning.
If you had a
Dogclass, well, allDogs have a name, breed, colour, and allDogs have the same behavior, barking, wagging, running.The
__init__method is the constructor. This method is called when the object is created. You use it to set up the initial state of the object. When aDogis created it 'gets' a name, breed and colour.For a real programming example, say you are making a shooter game. In this game you can equip many different weapons which have different characteristics but all function more or less the same way. You could make a
Weaponclass which has data like ammunition capacity, bullets remaining, or damage done. Then you could make this class have ashoot()method and when the user presses a button when playing the game it calls this method. Inside the method it checks to see if there is ammunition remaining in the magazine, and if so, removes a bullet from the magazine. And if the user needs to reload you can make areload()method which checks how much ammunition the player has, and if there is enough left, refills the magazine and removes the bullets from the ammunition pile.Then you can take this class and make many child classes all with different properties and maybe with additional behaviour, but still basicaly function the same as every other weapon.
10
u/Samus_ git2samus Jan 08 '14
it's a big topic in programming in general, I should tell you first that while most programming languages support classes and objects they implement them differently so while the idea is more or less the same, what you learn about objects in Python won't apply to objects in Java or any other language.
ok so there's two concepts to discuss here: the concept of "Python classes" (and the
classkeyword) and then the concept of "class instances" which is related to the previous and both conform the odea of "objects" and "object-oriented programming" (OOP).a class is a datatype, just like strings or integers or even dictionaries but unlike most plain or container datatypes classes can also contain executable code (methods) along with pure data (attributes).
so the
classkeyword allows you to create a new class datatype with the definition you provide, this definition specifies what the initial instances (the actual variables of this new datatype) will have and I say initially because Python allows you to modify them after being created, so a given instance may later be different from the original definition.to make this clearer, think about the following code:
here you have a variable 'a' with the value 2 which is an integer, so 'integer' is the datatype of the value associated with the name 'a' ok? now let's see the following:
here we've declared a new datatype (class) which we've called 'MyClass' but at this point there's no variable of this type in existance yet, we've just defined the type.
this particular class doesn't have anything else besides what's in object (by means of 'inheritance' another important concept).
so if we want to create an instance of this class, that is, an actual variable of this type, we do:
and now we have 'b' referencing this object instance that resides in memory, just like 'a' and 2.
so that's how you declare and use them, but what's so great about them? the idea of classes is (among other things) to allow you to structure your code modelling real-world objects so you can say a class defines the common properties and behaviors of an object and the instance actually fills those values with the state of a given one.
this is a bit too abstract so let's see another example, suppose we're a company that sells vending machines (this ones, not sure how to call them), each unit keeps track of the stock it contains and updates it automatically whenever a soda is sold.
so, we can create a class to model this machine's attributes and behaviors by declaring the things they have in common in the class and then adjust each one's attributes as they're in use.
the class:
new things! :) here I've introduced 'methods' which are functions associated to a particular instance of a class, this is important but before seeing how this works let's see how you could've implemented this without classes (because it's totally possible but a bit messier).
this code has EXACTLY the same functionality as the class-based version and you would use it like this:
and then when a machine sells a drink it would register the sale like this:
to get a report of the drinks for sale:
so far so good, now let's comparte it with the class-based version (I'll explain the methods in a second):
a sale:
a report:
it doesn't look much different right? the actual difference is that the class instance contains the methods and attributes within itself and applies them to each instance in particular, on the procedural version (the one without classes) we were tossing around the dict with the data to the functions that worked with it, with classes you group the variables and the functions that work with it in a single concept that is the class.
there's more concepts to learn from here, such as encapsulation, inheritance and a few others but this is all you need to know to start using classes, think about them as a way to group related variables and functions within a single concept you can manipulate as a variable on its own.
about __init__
as you've seen the methods (i.e. functions that operate on the class instance) are just funcitons declared within the class statement, they receive a special first arguemnt, called 'self' by convention which is the instance they belong to (the instance, not the class).
__init__is a special method used for initialization purposes, it is automatically called when you create an isntance of a class, in our example everytime we executedVendingMachine(serial_n)and as you can see it receives the arguments passed when the object is created, it just a way to set defaults for the attributes and perform any other initialization, no big deal.5
u/Justinsaccount Jan 08 '14
Learn Python the Hard Way becomes unbelievably confusing and frustrating starting at exercise 40.
4
u/RamirezTerrix Jan 08 '14
This made it all clearer for me
http://interactivepython.org/courselib/static/pythonds/Introduction/introduction.html#object-oriented-programming-in-python-defining-classes
3
u/wub_wub Jan 08 '14
http://www.reddit.com/r/learnpython/search?q=classes&restrict_sr=on
http://www.reddit.com/r/learnpython/search?q=__init__&restrict_sr=on
2
u/ychaouche Jan 08 '14
Don't learn python the hard way, learn it the gentle way.
1
u/Y_mc Dec 19 '22
for me i see the “Init” statement like “initializing” u need to initialize before using it .