First page Back Continue Last page Overview Graphics

Methods in Classes, 1

def hi(obj):

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

class Robot:

pass

x = Robot()

x.name = "Marvin"

hi(x)

def hi(obj):

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

class Robot:

say_hi = hi

x = Robot()

x.name = "Marvin"

Robot.say_hi(x)

We will now bind the function „hi“ to a class attribute „say_hi“!

“say_hi” is called a method.

Usually, it will be called like this:

x.say_hi()