#!/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()
(源文件:code/method.py)
輸出
$ python method.py
Hello, how are you?
它如何工作
這里我們看到了 self 的用法。注意 sayHi 方法沒有任何參數(shù),但仍然在函數(shù)定義時有 self。