检查Python中的成员是否存在
-
03-07-2019 - |
题
我经常想检查对象是否有成员。一个例子是在函数中创建单例。为此,您可以使用 hasattr
,如下所示:
class Foo(object):
@classmethod
def singleton(self):
if not hasattr(self, 'instance'):
self.instance = Foo()
return self.instance
但你也可以这样做:
class Foo(object):
@classmethod
def singleton(self):
try:
return self.instance
except AttributeError:
self.instance = Foo()
return self.instance
另一种方法更好吗?
编辑已添加 @classmethod
...但请注意,问题是不有关如何制作单身但如何检查在对象中存在成员。
编辑:对于该示例,典型用法是:
s = Foo.singleton()
然后 s
是 Foo
类型的对象,每次都是相同的。并且,通常,该方法被多次调用。
解决方案
这是两种不同的方法:№ 1是LBYL(在你跳跃之前看)和№ 2是EAFP(更容易请求宽恕而不是许可)。
Pythonistas通常建议EAFP更好,参数的风格为“如果一个进程在你测试它的时间和你自己创建它的时间之间创建文件会怎么样?”。这个论点不适用于此,但这是一般的想法。不应将异常视为 too 例外。
在您的情况下性能方面—因为在创建异常( raise
关键字)时,在CPython中设置异常管理器( try
关键字)非常便宜和内部异常创建)是相对昂贵的—使用方法№ 2异常只会被提出一次;之后,你只需使用该物业。
其他提示
我只是想测量时间:
class Foo(object):
@classmethod
def singleton(self):
if not hasattr(self, 'instance'):
self.instance = Foo()
return self.instance
class Bar(object):
@classmethod
def singleton(self):
try:
return self.instance
except AttributeError:
self.instance = Bar()
return self.instance
from time import time
n = 1000000
foo = [Foo() for i in xrange(0,n)]
bar = [Bar() for i in xrange(0,n)]
print "Objs created."
print
for times in xrange(1,4):
t = time()
for d in foo: d.singleton()
print "#%d Foo pass in %f" % (times, time()-t)
t = time()
for d in bar: d.singleton()
print "#%d Bar pass in %f" % (times, time()-t)
print
在我的机器上:
Objs created.
#1 Foo pass in 1.719000
#1 Bar pass in 1.140000
#2 Foo pass in 1.750000
#2 Bar pass in 1.187000
#3 Foo pass in 1.797000
#3 Bar pass in 1.203000
似乎try / except更快。它似乎对我来说更具可读性,无论如何取决于具体情况,这个测试很简单,也许你需要一个更复杂的测试。
这取决于哪种情况是“典型的”,因为例外情况应该是模型,嗯,非典型条件。因此,如果典型情况是 instance
属性应该存在,那么使用第二种代码样式。如果没有 instance
与 instance
一样典型,那么使用第一种样式。
在创建单例的特定情况下,我倾向于使用第一种样式,因为在初始时间创建单例是一个典型的用例。 : - )
使用它的方式有点偏离主题。单身人士被高估,并且“共享状态”被称为“共享状态”。方法在python中同样有效,而且大多数都非常干净,例如:
class Borg:
__shared_state = {}
def __init__(self):
self.__dict__ = self.__shared_state
# and whatever else you want in your class -- that's all!
现在每次都这样做:
obj = Borg()
它将具有相同的信息,或者具有相同的实例。
我必须同意克里斯的观点。请记住,在实际需要之前不要进行优化。我真的怀疑检查存在是否会成为任何合理程序的瓶颈。
我确实看到了 http://code.activestate.com/recipes/52558/也是这样做的一种方式。该代码的未注释副本(“垃圾邮件”只是类接口的随机方法):
class Singleton:
class __impl:
def spam(self):
return id(self)
__instance = None
def __init__(self):
if Singleton.__instance is None:
Singleton.__instance = Singleton.__impl()
self.__dict__['_Singleton__instance'] = Singleton.__instance
def __getattr__(self, attr):
return getattr(self.__instance, attr)
def __setattr__(self, attr, value):
return setattr(self.__instance, attr, value)