【发布时间】:2015-05-11 01:23:51
【问题描述】:
我有以下代码;
class weather(object):
temperature = 0
humidity = 0
precis = "There is no weather when there plainly should be, please contact an administrator."
cloud = "none"
windDirection = "nnw"
windSpeed = 0
def getWeather(self):
weatherJSON = requests.get('http://www.bom.gov.au/fwo/IDT60901/IDT60901.94970.json')
weatherDoc = json.loads(weatherJSON.text)
temperature = weatherDoc["observations"]["data"][1]["apparent_t"]
humidity = weatherDoc["observations"]["data"][1]["rel_hum"]
windDirection = weatherDoc["observations"]["data"][1]["wind_dir"]
windSpeed = weatherDoc["observations"]["data"][1]["wind_spd_kmh"]
cloud = weatherDoc["observations"]["data"][1]["cloud_type"]
这是一个具有天气特征的类,并包含一个更新它们的函数。如果我再用
实例化这个类this = weather()
this.getWeather()
其中的变量不会随着真实世界的天气而更新。两个问题,为什么不呢?我想这更像是一个子问题,我这样做对吗?我的方法应该是使用类中的方法来操作类的实例吗?
【问题讨论】:
-
附带说明,您确实应该使用一致的缩进,有时不是 2 个空格,有时是 4 个空格。它使您的代码更易于阅读和维护。