checking member existence python
i frequently wish check an vigilant member not. an instance origination essay function. purpose, hasattr
this:
class foo(object):
@classmethod
def singleton(self):
hasattr(self, 'instance'):
self.instance = foo()
relapse self.instance
but also this:
class foo(object):
@classmethod
def singleton(self):
try:
relapse self.instance
solely attributeerror:
self.instance = foo()
relapse self.instance
is slight improved other?
edit: total @classmethod
... nonetheless note doubt not essay nonetheless check coming member an object.
edit: example, customary use be:
s = foo.singleton()
then s
an vigilant form foo
, same any time. and, typically, slight called times.
Comments
Post a Comment