我从 udacity.com 看到了这个例子:
def say_hi():
return 'hi!'
i = 789
class MyClass(object):
i = 5
def prepare(self):
i = 10
self.i = 123
print i
def say_hi(self):
return 'Hi there!'
def say_something(self):
print say_hi()
def say_something_else(self):
print self.say_hi()
输出:
>>> print say_hi()
hi!
>>> print i
789
>>> a = MyClass()
>>> a.say_something()
hi!
>>> a.say_something_else()
Hi there!
>>> print a.i
5
>>> a.prepare()
10
>>> print i
789
>>> print a.i
123
我什么都懂,除了为什么a.say_something()
equalshi!
和 not Hi there!
。这对我来说很奇怪,因为它在之后say_something()
调用时调用的是类内部的say_hi()
。我想我错过了一些重要的东西..