ezoic

Friday, January 27, 2017

Another Python class example

class Rocket():
    def __init__(self):
            self.x=0
            self.y=0
    def move_up(self):
        self.y+=1
        return self.y


my_rockets=[]

for x in range(0,5):
        new_rocket=Rocket()
        my_rockets.append(new_rocket.move_up())

for rocket in my_rockets:
        print(rocket)


output:

1
1
1
1
1


If you change a little:

class Rocket():
    def __init__(self):
            self.x=0
            self.y=0
    def move_up(self):
        self.y+=1
        return self.y


my_rockets=[]

for x in range(0,5):
        new_rocket=Rocket()
        my_rockets.append(new_rocket)

for rocket in my_rockets:
        print(rocket)


output will be:

<_main_.Rocket instance at ox7f7fc3fa5878>
<_main_.Rocket instance at ox7f7fc3fa58c0>

...

each time, you got a new rocket.


No comments:

Post a Comment

looking for a man

 I am a mid aged woman.  I was born in 1980. I do not have any kid. no complicated  dating before . I am looking for a man here for marriage...