Python 使用类';更新类';变量?如果是,我该怎么做?

Python 使用类';更新类';变量?如果是,我该怎么做?,python,json,oop,object,Python,Json,Oop,Object,我有以下代码 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 = reque

我有以下代码

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()

其中的变量不会随真实天气而更新。有两个问题,为什么不呢?我想这更像是一个次要问题,我做得对吗?我的方法应该是使用类中的方法来操作类的实例吗?

示例中的缩进是否已损坏?这些变量应该是天气类的类成员吗

同样,该函数是隐藏它们,在本地创建新的,因此不会更改其他

有两个问题,为什么不呢?我想这更像是一个次要问题,我做得对吗

您正在更新getWeather()中的局部变量

您应该更新实例变量,这可以通过在变量名称前加上self来完成,换句话说,替换:

def getWeather(self):
    ...
    cloud = ...


这里有两个基本问题


首先,您混合了类属性(即,由同一类的所有实例共享的变量)和普通实例属性(即,作为每个实例一部分的变量)

您很少需要任何东西的类属性。但是,您始终需要实例属性。因此,首先:

class weather(object):
    def __init__(self):
        self.temperature = 0
        self.humidity = 0
        self.precis = "There is no weather when there plainly should be, please contact an administrator."
        self.cloud = "none"
        self.windDirection = "nnw"
        self.windSpeed = 0
注意
自身温度
,而不仅仅是
温度
。这就是创建实例属性的方式。这也是你如何访问或更新一个


其次,您还混合了局部变量,即函数运行时存在的变量,然后随实例属性一起消失。但您已经知道了从上面更新实例属性的方法。因此:

def getWeather(self):
    weatherJSON = requests.get('http://www.bom.gov.au/fwo/IDT60901/IDT60901.94970.json')
    weatherDoc = json.loads(weatherJSON.text)
    self.temperature = weatherDoc["observations"]["data"][1]["apparent_t"]
    self.humidity =  weatherDoc["observations"]["data"][1]["rel_hum"]
    self.windDirection = weatherDoc["observations"]["data"][1]["wind_dir"]
    self.windSpeed = weatherDoc["observations"]["data"][1]["wind_spd_kmh"]
    self.cloud = weatherDoc["observations"]["data"][1]["cloud_type"]
    self.precis = '???'
(我不确定您想在precis中输入什么,但很明显,您不想将其作为“没有天气…”来保留。)


如果您在没有第一个修复的情况下进行第二个修复,那么一切看起来都会正常工作,但这只是巧合。当您请求
this.temperature
时,如果
this
没有名为
temperature
的实例属性,Python将自动查找类属性
type(this.temperature
)。如果随后添加名为
temperature
的实例属性,它会“隐藏”class属性,因此下次执行
this.temperature
,您将获得实例的值


因此,您可以使用类属性作为实例属性的一种“默认值”。但你只有知道自己在做什么才应该这么做。(如果你开始在方法中使用列表等可变值并对其进行变异,这可能会让人感到困惑…

这不是一个真正的答案,要求澄清可能更好地作为注释来完成。此外,函数实际上并不隐藏类属性。作为补充说明,你真的应该使用一致的缩进,有时不是2个空格,有时是4个空格。它使您的代码更易于阅读和维护。
def getWeather(self):
    weatherJSON = requests.get('http://www.bom.gov.au/fwo/IDT60901/IDT60901.94970.json')
    weatherDoc = json.loads(weatherJSON.text)
    self.temperature = weatherDoc["observations"]["data"][1]["apparent_t"]
    self.humidity =  weatherDoc["observations"]["data"][1]["rel_hum"]
    self.windDirection = weatherDoc["observations"]["data"][1]["wind_dir"]
    self.windSpeed = weatherDoc["observations"]["data"][1]["wind_spd_kmh"]
    self.cloud = weatherDoc["observations"]["data"][1]["cloud_type"]
    self.precis = '???'