hasattr Questions

13

Solved

if hasattr(obj, 'attribute'): # do somthing vs try: # access obj.attribute except AttributeError, e: # deal with AttributeError Which should be preferred and why?
Colley asked 24/5, 2009 at 5:11

6

Solved

I have a dictionary that sometimes receives calls for non-existent keys, so I try and use hasattr and getattr to handle these cases: key_string = 'foo' print "current info:", info print hasattr(in...
Historiography asked 23/5, 2012 at 17:13

2

Solved

Given the following class (with a buggy property) then what is the best foolproof way of checking that the bar property exists? class Foo(object): @property def bar(self): raise AttributeError(...
Reflexion asked 19/5, 2016 at 8:0

1

Solved

Why does hasattr say that the instance doesn't have a foo attribute? >>> class A(object): ... @property ... def foo(self): ... ErrorErrorError ... >>> a = A() >>> hasat...
Cholecalciferol asked 23/2, 2016 at 0:37

2

I have a class that implements virtual attributes using __getattr__. The attributes can be expensive, e.g. performing a query. Now, I am using a library which checks if my object has the attribute ...
Willi asked 17/5, 2015 at 18:15

3

Solved

hasattr documentation says that it takes an object and an attribute name and lets you know if that attribute exists on that object. I have discovered that it seems to work on class names too (i.e....
Pepin asked 27/4, 2015 at 16:5

6

if hasattr(form, 'name') and hasattr(form, 'date'): print(form.name) #'Some name' - True print(form.date) #none - False This condition validates as True even if the hasattr(form, 'date') is fal...
Spaniel asked 12/12, 2013 at 12:18

5

Solved

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): @classmeth...
Beene asked 15/10, 2008 at 10:54
1

© 2022 - 2024 — McMap. All rights reserved.