Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for member existence in Python

I regularly want to check if an object has a member or not. An example is the creation of a singleton in a function. For that purpose, you can use hasattr like this:

class Foo(object):     @classmethod     def singleton(self):         if not hasattr(self, 'instance'):             self.instance = Foo()         return self.instance 

But you can also do this:

class Foo(object):     @classmethod     def singleton(self):         try:             return self.instance         except AttributeError:             self.instance = Foo()             return self.instance 

Is one method better of the other?

Edit: Added the @classmethod ... But note that the question is not about how to make a singleton but how to check the presence of a member in an object.

Edit: For that example, a typical usage would be:

s = Foo.singleton() 

Then s is an object of type Foo, the same each time. And, typically, the method is called many times.

like image 623
PierreBdR Avatar asked Oct 15 '08 10:10

PierreBdR


People also ask

How do I check if a variable exists in Python?

To check the existence of a local variable: if 'myVar' in locals(): # myVar exists. To check the existence of a global variable: if 'myVar' in globals(): # myVar exists.

How do you check if a variable has been initialized Python?

Python doesn't have a specific function to test whether a variable is defined, since all variables are expected to have been defined before use, even if initially assigned the None object.

How do you check something in Python?

To check if the list contains an element in Python, use the “in” operator. The “in” operator checks if the list contains a specific item or not. It can also check if the element exists on the list or not using the list.

How do you check if an input is in a list Python?

count() to check if the list contains. Another built-in method in Python, count() returns the number of times the passed element occurs in the list. If the element is not there in the list then the count() will return 0. If it returns a positive integer greater than 0, it means the list contains the element.


1 Answers

These are two different methodologies: №1 is LBYL (look before you leap) and №2 is EAFP (easier to ask forgiveness than permission).

Pythonistas typically suggest that EAFP is better, with arguments in style of "what if a process creates the file between the time you test for it and the time you try to create it yourself?". This argument does not apply here, but it's the general idea. Exceptions should not be treated as too exceptional.

Performance-wise in your case —since setting up exception managers (the try keyword) is very cheap in CPython while creating an exception (the raise keyword and internal exception creation) is what is relatively expensive— using method №2 the exception would be raised only once; afterwards, you just use the property.

like image 55
tzot Avatar answered Oct 05 '22 03:10

tzot