属性丢失时的python调用集属性
我有一个对象,它有几个属性,其中包含的值需要一段时间才能查询。因此,我不想在创建实例时获取这些属性的所有值,而是在代码路径实际需要该属性时,因为根据代码路径,只需要少数属性。在eh代码中达到某些点的顺序也不是很确定,因此我无法在脚本中的固定点设置属性。所以我要创建一个方法属性丢失时的python调用集属性,python,Python,我有一个对象,它有几个属性,其中包含的值需要一段时间才能查询。因此,我不想在创建实例时获取这些属性的所有值,而是在代码路径实际需要该属性时,因为根据代码路径,只需要少数属性。在eh代码中达到某些点的顺序也不是很确定,因此我无法在脚本中的固定点设置属性。所以我要创建一个方法 def getValue(self, attributeName): if hasattr(self, attributeName): return getattr(self, attributeNa
def getValue(self, attributeName):
if hasattr(self, attributeName):
return getattr(self, attributeName)
elif attributeName == 'A1':
v = ... code to get value for A1
self.A1 = v
return v
elif attributeName == 'A2':
v = ... code to get value for A2
self.A2 = v
return v
....
但我想知道这是否真的是处理这个问题的一个好方法,或者是否有一些更聪明的方法更可取。
感谢您的评论您可以这样使用:
class Foo:
def __init__(self):
# ordinary attributes
self.B1 = something
self.B2 = something_else
@property
def A1(self):
try:
return self._A1
except AttributeError:
self._A1 = ....calculate it....
return self._A1
然后你可以:
foo = Foo()
print foo.A1 # attribute calculated when first used
print foo.A1 # this time, the value calculated before is used
您可以这样使用:
class Foo:
def __init__(self):
# ordinary attributes
self.B1 = something
self.B2 = something_else
@property
def A1(self):
try:
return self._A1
except AttributeError:
self._A1 = ....calculate it....
return self._A1
然后你可以:
foo = Foo()
print foo.A1 # attribute calculated when first used
print foo.A1 # this time, the value calculated before is used
您可以这样使用:
class Foo:
def __init__(self):
# ordinary attributes
self.B1 = something
self.B2 = something_else
@property
def A1(self):
try:
return self._A1
except AttributeError:
self._A1 = ....calculate it....
return self._A1
然后你可以:
foo = Foo()
print foo.A1 # attribute calculated when first used
print foo.A1 # this time, the value calculated before is used
您可以这样使用:
class Foo:
def __init__(self):
# ordinary attributes
self.B1 = something
self.B2 = something_else
@property
def A1(self):
try:
return self._A1
except AttributeError:
self._A1 = ....calculate it....
return self._A1
然后你可以:
foo = Foo()
print foo.A1 # attribute calculated when first used
print foo.A1 # this time, the value calculated before is used
您可以使用此装饰器:
class cached_property(object):
"""Define property caching its value on per instance basis.
Decorator that converts a method with a single self argument into a
property cached on the instance.
"""
def __init__(self, method):
self.method = method
def __get__(self, instance, type):
res = instance.__dict__[self.method.__name__] = self.method(instance)
return res
这是一种解释。您可以使用此装饰器:
class cached_property(object):
"""Define property caching its value on per instance basis.
Decorator that converts a method with a single self argument into a
property cached on the instance.
"""
def __init__(self, method):
self.method = method
def __get__(self, instance, type):
res = instance.__dict__[self.method.__name__] = self.method(instance)
return res
这是一种解释。您可以使用此装饰器:
class cached_property(object):
"""Define property caching its value on per instance basis.
Decorator that converts a method with a single self argument into a
property cached on the instance.
"""
def __init__(self, method):
self.method = method
def __get__(self, instance, type):
res = instance.__dict__[self.method.__name__] = self.method(instance)
return res
这是一种解释。您可以使用此装饰器:
class cached_property(object):
"""Define property caching its value on per instance basis.
Decorator that converts a method with a single self argument into a
property cached on the instance.
"""
def __init__(self, method):
self.method = method
def __get__(self, instance, type):
res = instance.__dict__[self.method.__name__] = self.method(instance)
return res
这是一个解释。使用这些:使用这些:使用这些:使用这些:谢谢,听起来和我在找的东西一模一样,听起来和我在找的东西一模一样,听起来和我在找的东西一模一样。老实说,我不明白。该类将如何与我的对象交互?使用此装饰器装饰类的方法。例如
@cached\u属性;def A1(自身):
。然后你会得到你描述的行为。您可以根据需要装饰任意多个方法,而无需在每个方法中摆弄try:except AttributeError:
。此装饰器使用“我不确定是否遵循”部分创建缓存属性。是否使用该值覆盖该方法,以便在随后调用该方法时,该方法将返回该值,而不再执行查询该值的方法?是的,完全正确。我在互联网上发现了这种模式,但找不到来源。老实说,我不明白这一点。该类将如何与我的对象交互?使用此装饰器装饰类的方法。例如@cached\u属性;def A1(自身):
。然后你会得到你描述的行为。您可以根据需要装饰任意多个方法,而无需在每个方法中摆弄try:except AttributeError:
。此装饰器使用“我不确定是否遵循”部分创建缓存属性。是否使用该值覆盖该方法,以便在随后调用该方法时,该方法将返回该值,而不再执行查询该值的方法?是的,完全正确。我在互联网上发现了这种模式,但找不到来源。老实说,我不明白这一点。该类将如何与我的对象交互?使用此装饰器装饰类的方法。例如@cached\u属性;def A1(自身):
。然后你会得到你描述的行为。您可以根据需要装饰任意多个方法,而无需在每个方法中摆弄try:except AttributeError:
。此装饰器使用“我不确定是否遵循”部分创建缓存属性。是否使用该值覆盖该方法,以便在随后调用该方法时,该方法将返回该值,而不再执行查询该值的方法?是的,完全正确。我在互联网上发现了这种模式,但找不到来源。老实说,我不明白这一点。该类将如何与我的对象交互?使用此装饰器装饰类的方法。例如@cached\u属性;def A1(自身):
。然后你会得到你描述的行为。您可以根据需要装饰任意多个方法,而无需在每个方法中摆弄try:except AttributeError:
。此装饰器使用“我不确定是否遵循”部分创建缓存属性。是否使用该值覆盖该方法,以便在随后调用该方法时,该方法将返回该值,而不再执行查询该值的方法?是的,完全正确。我在网上找到了这个模式,但找不到来源