Python:无法从不同的 modules/functions 访问 class 属性

Python: cannot access class attribute from different modules/functions

我有一个名为 Org 的 class,我正在尝试从多个函数(定义在 class 之外)访问它的方法。我先呼叫 main(),然后呼叫 discover_buildings()main() 执行时没有错误,但是,我在调用 discover_buildings() 后得到 AttributeError: 'Org' has no attribute 'headers' 错误。我做错了什么? (我期待 headers 属性在不同方法之间共享)

class Org(object):

    def __init__(self, client_id, client_secret, grant_type='client_credentials'):
        self.grant_type = grant_type
        self.client_id = client_id
        self.client_secret = client_secret
        self.url = CI_A_URL

    def auth(self):
        """ authenticate with bos """
        params = {
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'grant_type': self.grant_type
        }
        r = requests.post(self.url + 'o/token/', data=params)
        if r.status_code == 200:
            self.access_token = r.json()['access_token']
            self.headers = {
                'Authorization': 'Bearer %s' %self.access_token,
                'Content-Type': 'application/json',
            }
        else:
            logging.error(r.content)
            r.raise_for_status()

    def get_buildings(self, perPage=1000):
        params = {
            'perPage': perPage
        }

        r = requests.get(self.url + 'buildings/', params=params, headers=self.headers)
        result = r.json().get('data')
        if r.status_code == 200:
            buildings_dict = {i['name']: i['id'] for i in result}
            sheet_buildings['A1'].value = buildings_dict
        else:
            logging.error(r.content)
            r.raise_for_status()


client_id = 'xxx'
client_secret = 'yyy'
gateway_id = 123
o = Org(client_id, client_secret)

def discover_buildings():
    return o.get_buildings()

def main():
    return o.auth()

在此先感谢您的帮助!

您没有定义 self.headers。在 运行 o.get_buildings().

之前,您需要 运行 o.auth()(或定义 self.headers

问题是您定义“discover_buildings”的方式 你首先用“o”定义它只是在认证之后初始化。

处理这个:

  1. 重写 discover 以将 'o' 作为参数

  2. 先检查 'o' 是否有 'headers' 如果未验证 'o' 并执行其余操作

     def discover_buildings():
         if not getattr(o, 'headers'):
             o.auth()
         return o.get_buildings()
    

尝试使用 属性 在需要时计算 headers 然后缓存它。


    def auth(self):
        """ authenticate with bos """
        #  you might want to isolate `token` into a nested @property token
        params = {
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'grant_type': self.grant_type
        }

        # note assignment to `_headers`, not `headers`



        r = requests.post(self.url + 'o/token/', data=params)
        if r.status_code == 200:
            self._access_token = r.json()['access_token']


        #   
            self._headers = { # 
                'Authorization': 'Bearer %s' %self._access_token,
                'Content-Type': 'application/json',
            }
        else:
            logging.error(r.content)
            r.raise_for_status()

    #cache after the first time.
    _headers = None
    
    @property
    def headers(self):
        """ call auth when needed
        you might want to isolate `token`
        into its own property, allowing different
        headers to use the same token lookup
        """
        if self._headers is None:
            self.auth()
        return self._headers