Python 3.x 来自同一类方法的Python调用类属性
我有class方法wait_for_操作,我想从中调用class属性并生成一个新的客户机,我想使用class方法Python 3.x 来自同一类方法的Python调用类属性,python-3.x,oop,kubernetes,Python 3.x,Oop,Kubernetes,我有class方法wait_for_操作,我想从中调用class属性并生成一个新的客户机,我想使用class方法 def get_new_bearer_token(): return "new_token" class MyClass(object): def __init__(self, json): self.json = json @property def client(self): k8_loade
def get_new_bearer_token():
return "new_token"
class MyClass(object):
def __init__(self, json):
self.json = json
@property
def client(self):
k8_loader = kube_config.KubeConfigLoader(self.json)
call_config = type.__call__(Configuration)
k8_loader.load_and_set(call_config)
Configuration.set_default(call_config)
return client
@classmethod
def wait_for_operation(self, kube_config, cloud):
while True:
if cloud == "AWS":
kube_config['users'][0]['user']['token'] = get_new_bearer_token()
self.json = kube_config
#call class property to generate a new client with token and use it
if __name__ == '__main__':
kube_config = {}
my_client = MyClass(json=kube_config)
my_client.client.CoreV1Api().list_namespace(watch=False)
result = MyClass.wait_for_operation(kube_config=kube_config,cloud='AWS')
使用改变状态或有副作用的属性是一个非常糟糕的主意。属性表示实例数据的视图,这就是为什么我们不真正说“调用属性”,而是说“获取或设置属性” 我的建议-删除
@property
装饰器,并将客户端
变成一个普通方法。给它起个更好的名字,比如generate\u new\u client
。然后像调用任何其他方法一样调用它-self.generate\u new\u client()