A Byte of Python

object Methods

We have already discussed that classes/objects can have methods just like functions except that we have an extra self variable. We will now see an example.

Using Object Methds

Example 11.2. Using Object Methods

				
#!/usr/bin/python
# Filename: method.py

class Person:
	def sayHi(self):
		print 'Hello, how are you?'

p = Person()
p.sayHi()

# This short example can also be written as Person().sayHi()
				
				

Output

				
$ python method.py
Hello, how are you?
				
				

How It Works

Here we see the self in action. Notice that the sayHi method takes no parameters but still has the self in the function definition.