Tuesday, October 8, 2013

First Post 9/25: Hard Coded

9/25: Hard Coded
From exercise 1 I learned how to call a function inside a definition
First you need to call the class name. Afterwards attach the function name to class name, followed by(). For example,


class Toy(object):
    def play(self):
        print ("Squeak!")
class Dog(object):
   
    def __init__(self, name):
        self.name = name
       
    def play(self,  toy, n):
        if n < 0:
            n = 0
        for i in range(n):
print( “Yip! ”, end=’’)
toy.play()

When the dog playing the toy, he speaks base on the toy he plays with. Therefore, I want the output from class Toy, which is “Squeak!”. However if I just print(self.speak Squeak!) this is a hard-coded.

Hard coding is simply embedding input data directly into the code of a program instead of getting the data from an external source. That means the data inputed cannot be change. If we give the dog another toy he can't speaks anything different. Hence we need to call toy.play() under dog.play() function.





No comments:

Post a Comment