如何更新 Python 实例属性
How to update a Python instance attribute
我在更新实例属性时遇到问题。
首先,我想初始化一个属性为None
其次,我想运行一个函数。如果功能成功,我想更新属性。如果功能不成功,return None.
我试过使用 @property
和 @contents.setter
装饰器,但似乎出错了。为了避免混淆,我删除了装饰器。
class OlympicAthlete:
def __init__(self, name):
self.url = f'https://www.olympic.org/{name}'
self.contents = None
def contents(self):
r = requests.get(self.url)
if r.status_code != 200:
print(f'Your get request was unsuccessful with a {r.status_code} error code')
return None
self.contents = r.text
phelps = OlympicAthlete('michael-phelps')
phelps.contents()
print(phelps.contents)
调用 print(phelps.contents)
后,我期待看到该页面的 html 文本。
非常感谢帮助!
问题是您正在尝试创建一个与 属性 同名的方法。您必须重命名其中之一。
import requests
class OlympicAthlete:
def __init__(self, name):
self.url = f'https://www.olympic.org/{name}'
self.contents = None
def method(self):
r = requests.get(self.url)
if r.status_code != 200:
print(f'Your get request was unsuccessful with a {r.status_code} error code')
else:
self.contents = r.text
phelps = OlympicAthlete('michael-phelps')
phelps.method()
print(phelps.contents)
但是如果你想使用装饰器...
import requests
class OlympicAthlete:
def __init__(self, name):
self.url = f'https://www.olympic.org/{name}'
@property
def contents(self):
r = requests.get(self.url)
if r.status_code != 200:
print(f'Your get request was unsuccessful with a {r.status_code} error code')
return None
return r.text
phelps = OlympicAthlete('michael-phelps')
print(phelps.contents)
初始化 OlympicAthlete
class 后,方法 contents
将被 None 覆盖。这就是为什么调用 contents
方法会导致错误,因为 NoneType 对象不可调用。
因此,您需要首先以不同于方法的方式命名 contents
属性,因为一个会覆盖另一个。
我建议按照
class OlympicAthlete:
def __init__(self, name):
self.url = f'https://www.olympic.org/{name}'
self.contents = None
def get_content(self):
r = requests.get(self.url)
if r.status_code != 200:
print(f'Your get request was unsuccessful with a {r.status_code} error code')
return None
self.contents = r.text
return self.contents
phelps = OlympicAthlete('michael-phelps')
phelps.get_content()
print(phelps.contents)
在这种情况下,您将获得两次结果(在连接成功的情况下)。
我会建议一种方法,它只会得到结果,或者 return 它或者将它分配给 class 属性,而不是两者。
def get_content(self):
r = requests.get(self.url)
if r.status_code != 200:
print(f'Your get request was unsuccessful with a {r.status_code} error code')
return None
return r.text
我在更新实例属性时遇到问题。
首先,我想初始化一个属性为None
其次,我想运行一个函数。如果功能成功,我想更新属性。如果功能不成功,return None.
我试过使用 @property
和 @contents.setter
装饰器,但似乎出错了。为了避免混淆,我删除了装饰器。
class OlympicAthlete:
def __init__(self, name):
self.url = f'https://www.olympic.org/{name}'
self.contents = None
def contents(self):
r = requests.get(self.url)
if r.status_code != 200:
print(f'Your get request was unsuccessful with a {r.status_code} error code')
return None
self.contents = r.text
phelps = OlympicAthlete('michael-phelps')
phelps.contents()
print(phelps.contents)
调用 print(phelps.contents)
后,我期待看到该页面的 html 文本。
非常感谢帮助!
问题是您正在尝试创建一个与 属性 同名的方法。您必须重命名其中之一。
import requests
class OlympicAthlete:
def __init__(self, name):
self.url = f'https://www.olympic.org/{name}'
self.contents = None
def method(self):
r = requests.get(self.url)
if r.status_code != 200:
print(f'Your get request was unsuccessful with a {r.status_code} error code')
else:
self.contents = r.text
phelps = OlympicAthlete('michael-phelps')
phelps.method()
print(phelps.contents)
但是如果你想使用装饰器...
import requests
class OlympicAthlete:
def __init__(self, name):
self.url = f'https://www.olympic.org/{name}'
@property
def contents(self):
r = requests.get(self.url)
if r.status_code != 200:
print(f'Your get request was unsuccessful with a {r.status_code} error code')
return None
return r.text
phelps = OlympicAthlete('michael-phelps')
print(phelps.contents)
初始化 OlympicAthlete
class 后,方法 contents
将被 None 覆盖。这就是为什么调用 contents
方法会导致错误,因为 NoneType 对象不可调用。
因此,您需要首先以不同于方法的方式命名 contents
属性,因为一个会覆盖另一个。
我建议按照
class OlympicAthlete:
def __init__(self, name):
self.url = f'https://www.olympic.org/{name}'
self.contents = None
def get_content(self):
r = requests.get(self.url)
if r.status_code != 200:
print(f'Your get request was unsuccessful with a {r.status_code} error code')
return None
self.contents = r.text
return self.contents
phelps = OlympicAthlete('michael-phelps')
phelps.get_content()
print(phelps.contents)
在这种情况下,您将获得两次结果(在连接成功的情况下)。 我会建议一种方法,它只会得到结果,或者 return 它或者将它分配给 class 属性,而不是两者。
def get_content(self):
r = requests.get(self.url)
if r.status_code != 200:
print(f'Your get request was unsuccessful with a {r.status_code} error code')
return None
return r.text