Sunday, July 28, 2013

Objects

     Lists gather variables and values together to use repeatedly. Functions gather code together to use repeatedly. And then there's objects. No, not the chair or couch you may be sitting on or the computer you're using, but objects which collect functions and other data together.
     Real world objects have things you can do with them, or actions. They also have certain attributes, or properties.  This same rule applies with Python as well.
     In Python, characteristics you know about an object are attributes. Things you can do with them are methods.
     Let's say we were making a ball in Python. The ball would have attributes and methods. The attributes would look like this:

ball.color
ball.size
ball.weight

     The methods would look like this:

ball.kick()
ball.throw()

     Etc.
     The attributes of the object are whatever you know, or will know about it. They're bits and pieces of information, which are numbers, strings, etc. Variables. Variables that are inside the object.
     The methods of the object are something you can call to do something. Functions. Functions that are inside an object.
     So, attributes are information, and methods are actions. Objects collect those two together. Alright, now that you hopefully understand what objects are by now, let's start making some!
     We need to make an object with these steps: first, we make the class. The class keyword tells Python IDLE you are making an object. We use the class to make an instance, or the object. You define what the object looks like, and then you put your object to use. Let's write this program in a new window!

     class Ball:

    def bounce(self):
        if self.direction == "down":
            self.direction = "up"

     Alright, here's our object. That self keyword is a big thing in making games. This is a hint that we're getting to the big stuff soon!
     So, all we have right now is a class definition for a ball with one method, bounce(). There are no attributes because they really don't belong to the class, but its instance. Hey, speaking of instances, why don't we give our object some?
     If we want an instance of our Ball, we'd do something like this:

theBall=Ball()

     We have no attributes for our ball, so let's get that taken care of.

theBall.size="small"
theBall.color="purple"
theBall.direction="down"

     Alright, we've got the attributes covered. Now, let's see our bounce() method again.
     This is how we would use it.

theBall.bounce()

     Okay, looking good. Let's put all of this in our program. We will also add in some print statements so we'll know what's happening.

class Ball:

    def bounce(self):
        if self.direction == "down":
            self.direction = "up"
         
theBall=Ball()
theBall.size="small"
theBall.color="purple"
theBall.direction="down"


print("The ball has been loaded.")
print("The ball is", theBall.size)
print("The ball is", theBall.color)
print("The ball is going", theBall.direction)
print("Let's bounce the ball.")
theBall.bounce()
print("The ball is now going", theBall.direction)

     Now let's test this. Run the program and see what happens.

>>> ================================ RESTART ================================
>>>
The ball has been loaded.
The ball is small
The ball is purple
The ball is going down
Let's bounce the ball.
The ball is now going up

     The bounce() function changed our balls direction! That's pretty cool, don't you think?
     That's about it for this article. See you later!

No comments:

Post a Comment