我想知道如何获取在类中声明为局部变量的字典的值?看看我下面的来源。注意:如果我在函数 get_current_weather 中声明这个变量,它就可以正常工作
class Weather(object):
weather = {
'January' : 'cold',
'Febrary' : 'cold'
}
def get_current_weather(self):
print weather['January']
weather = Weather()
weather.get_current_weather()
终端错误:
Traceback (most recent call last):
File "game.py", line 27, in <module>
weather.get_current_weather()
File "game.py", line 24, in get_current_weather
print weather['January']
**TypeError: 'Weather' object is not subscriptable**