Monday, July 25, 2011

Python Classes Revision

#class example
class Student:
    def __init__(self, myname, myage):  # note double underscores
        self.myname = myname                 
# take passed value to a var
        self.myage = myage                   # take next value
    def sayname(self):                            # take next function in class
        print "Hello, my name is", self.myname

    def sayage(self):
        print "Hello, my age is", self.myage

    def jump(self,jump):  # the jump function
        if (jump > 0) :
            print "I am jumping up ", jump
        else :
            print "I am jumping down ", jump

name1 = Student("Amara", 17) # create object from class
name1.sayname() # print name
name1.sayage()
name1.jump(4) # move up 4
name1.jump(- 4)
# move down 4

No comments:

Post a Comment