Hi, I hope I'm not too late to help.
So from what I understand from your question is that you are in principle comfortable with the concept of a class, but let me wrap it up to get things straight here:
In object oriented programming, which you are doing when using classes (awesome!), you try to abstract a certain functionality of your program into what is called a class. A class is a collection of variables and functions that work together. More specifically a class is a template to create objects (of that class). Programming with classes (or object oriented programming) is a good way to model things that exist in the real world in the computer.
Here is a way to think when modeling the real world in classes (e.g. in a solar system):
Categorize the things that you want to model: You want to have planets in your game? That is a pretty good category of things with certain properties. Make it a class!
For each class, think about its properties in general: Planets have a name, a size, maybe a surface temperature, distance to the sun, etc. Make these class-variables
Now think about what your "thing" can do or how it can act in your model: A planet for example could rotate on its axis or a spaceship could land on a planet. Make these actions class methods (functions).
Now here is the fun part: Once you have build a class (let's say
Planet) you can use and reuse it to create concrete objects of that class. So you can create Jupiter from the Planet function and Saturn and Pluto - wait that's not a Planet! You get the idea. To do this you specify the actual values of the things you defined in your class. Remember: The class is a template for creating many different objects that belong to that class. The objects can then work independently with their own attributes and methods. Objects are also refered as instances of a class.
So let's see how that comes together in python: (Note: I generally use python 3 syntax - just a heads up, but that does not really matter here I guess...)
Just look at this sample class:
def __init__(self, n, s, t):
self.name = n
self.size = s
self.temperature = t
Planet only contains one function and it is a special one you asked about as well: The
__init__ function, also called the constructor. It is responsible for creating an actual object from your class definition. Note that it takes four parameters:
n s t and the special first one
self. It then saves the parameters into class-variables (indicated by the
self.-part. In that way the values you give to the constructor are associated with an object of the class which is implicitly returned. "Whaat?" Let's just do this:
planet1 = Planet('Jupiter', 70000, -145) # Note: km and °C - let's be scientific :)
You now have created a planet-object by just calling the constructor of the planet-class which is in python just called by using the class name used like a functions with parameters for the
__init__ function. The object is stored in the variable
In your code you can then access these values with the
In that way you can go on with this and create all the planets with all the attributes you want from that one single class and reuse the same code again and again.
The whole thing comes into its own when you add class-methods. Let's say you want also an astronaut-class to model your players character in the game. Let's give him or her some attributes:
def __init__(self, name='Player1', oxygen=100, health=100, knowledge=):
self.name = name
self.oxygen = oxygen
self.health = health
self.knowledge = knowledge
Note that this time we gave the constructor some default values that make sense. In this way you can create a generic 'John Doe' astronaut by just calling
Astronaut(). You still can overwrite all of those parameter of course, just pass them into the constructor.
Let's add a function to let the astronaut do things. Inside the
def explore(self, planet):
n = planet.name
t = planet.temperature
s = planet.size
self.knowledge.append([n, t, s])
The explore function takes a planet as its parameter and extracts the information that is stored in the planet object. It then adds it to the knowledge list of the astronaut that actually explored it i.e. that called the function! You see how the function is bound to its caller, the astronaut object by the
self. So if you got multiple astronauts they can independently explore different planets and build up different knowledge lists using the same code from the astronaut class.
This is a big and exciting topic and it is not easy, but once you get it it's a really nice way of designing code. Python is a great language but its object model is not the easiest to understand. Think of
self as a way of the class (again the template) to refer to the actual object that will be created when the construtor is called.
I hope I could help a little bit. Feel free to ask more specific stuff. This is actually MY first time responding to a tech question on the interwebs - so I am pretty curious if you find this helpful.
So long - Keep it up!