In the last program we did, we used a function known as __init__(). To help make programming easier, we have to know what the functions even mean. So, what in the heck does this one mean?
Alright, you could actually just look back an article and figure it out. But, if you don't want to or still don't get it, here's what I would say:
An instance of theBall (in the last article) is Ball2. __init__() will run whenever an instance is created, so Ball2 activates it. You are able to pass arguments to __init__() to make the instance with the properties set any way you want it to be. You could always look at the Ball2.py program to help jog your memory, too. I'm not gonna lie, this is sort of confusing. But, we only used it to set the object's properties, and that's probably the only thing it is used for.
There's not just __init__(), there are many functions of that type. What I'm talking about are __xxxx__() functions. We will cover them next time!
Saturday, August 31, 2013
Tuesday, August 6, 2013
A Quick Tip - Initializing
In our Ball.py program, we created the attributes after the object was created. However, there's a way to create the attributes, or properties, when the object is being created. It's called initializing, with the __init__() function.
I'm not quite sure (yet) how useful adding properties to an object when it's created is, But we might find a use for it in future articles.
Remember: this function uses four underscores (_) and not just two!
Alright, let's make a copy of our ball program, but this time, we will use __init__().
class Ball2:
def __init__(self, color, size, direction):
self.color=color
self.size=size
self.direction=direction
def bounce(self):
if self.direction == "down":
self.direction = "up"
theBall=Ball2("yellow", "large", "down")
print("The ball has been loaded.")
print("The ball is", theBall.color)
print("The ball is", theBall.size)
print("The ball is going", theBall.direction)
print("Let's bounce the ball.")
theBall.bounce()
print("The ball is now going", theBall.direction)
It will run exactly the same as our first ball program. This will be a handy tip later on! I'm sure of it!
I'm not quite sure (yet) how useful adding properties to an object when it's created is, But we might find a use for it in future articles.
Remember: this function uses four underscores (_) and not just two!
Alright, let's make a copy of our ball program, but this time, we will use __init__().
class Ball2:
def __init__(self, color, size, direction):
self.color=color
self.size=size
self.direction=direction
def bounce(self):
if self.direction == "down":
self.direction = "up"
theBall=Ball2("yellow", "large", "down")
print("The ball has been loaded.")
print("The ball is", theBall.color)
print("The ball is", theBall.size)
print("The ball is going", theBall.direction)
print("Let's bounce the ball.")
theBall.bounce()
print("The ball is now going", theBall.direction)
It will run exactly the same as our first ball program. This will be a handy tip later on! I'm sure of it!
Subscribe to:
Posts (Atom)