Python by Swaroop C H - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

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 theself in action. Notice that thesayHi method takes no parameters but still has the self in the function definition.