Ok! Object oriented programming, the art of building black boxes.
Let's say we wanted to make a game, something small and simple. Let's say the goal of this game is to guess a random number, and each player has their own number to try and guess. We also want to keep track of the name of each player so we know who is guessing, so let's list some of the requirements.
- We have players
- Each player has a name
- Each player has a hidden number
- Each player can make a guess at their number.
Now the second thing on that list means we are going to need strings, and the third means integers, somewhere in the program. The fourth thing looks like a function, but what about the first? It is more like a thing to contain the other three items in out list. It is going to be an object!
We can use objects to group different values together, and define methods to act on these values. How about we test this out with a small prototype object.
class Player:
def __init__ (self):
self.name = raw_input('Please enter player name: ')
a = Player()
print a.name
Well... Let's take a step back and look at what we did. Firstly we started with
class Player:
Just like you define functions with def
(name), we define a class with class (name). We could have used any name we want, but it is important to always keep class names descriptive.
Then we move onto
def __init__ (self):
Well you should be able to tell from the 'def' keyword that we are defining a function, but '__init__' (Those are double _ on either side, by the way) means we are making a very special function. This function will be called when ever we make a new object, and it is called the constructor. Think of it as the function that is used to build the new object. The
really weird bit here is use of another keyword, 'self'. Don't worry too much about this, just accept for now that it is black magic, and all methods defined in a class need this. This isn't exactly true, but I can explain where you do and don't need it later on, and for now you will always need it.
Next up,
self.name = raw_input('Please enter player name: ')
Hopefully you will have seen the second half of that before, it just gets some input from who ever is using the computer, but the first half is interesting. There is that 'self' keyword again. In this case, because we are building an object, we are saying that this objects that we are building in this method (A.K.A self) has a value called 'name', and giving it a value. We will be able to use name later.
And that is it for making our class, but not for using objects. I should explain one from the other...
A class is a blueprint, or the abstract ideal of something. An object is a real thing that you can see and touch and hold. For example, in real life, 'building' is a class, but the building you are sitting in right now is an object. Building is sort of this abstract term that can be used to describe a lot of real things, to help us understand those things, while your house is a real object that can be interacted with. It is very much the same in programming. Player is just a concept, but we don't have any players yet, nor do we have any integers or strings or anything else, so let's make one!
a = Player()
When I called Player(), it made a new object and called the __init__ method. 'a' is now an object, that we can see and touch and use! So let's use it!
print a.name
Remember how we told it what it's name was? Well it still knows, and we can use that as any other variable. Here we are just having it print it's name.
Continues in part 2.PREPOST EDIT:
Why yes! I do believe there is such a way using return types. For example
def ModString(s):
s += " Dawg"
return s
myString = "Hello"
print myString
myString = ModString(myString)
print myString
Need further help with this? If you have never used return types before I might need to give a better explanation.