First page Back Continue Last page Overview Graphics

Methods in Classes, 2

class Robot:

def say_hi(self):

print("Hi, I am " + self.name)

x = Robot()

x.name = "Marvin"

x.say_hi()

# we can also call it like this:

Robot.say_hi(x)

Problem of the previous class definition:

After having initialised a Robot robot, we shouldn't forget to assign a name to the robot in an additional statement.