hasattr

Detect if a __getattribute__ call was due to hasattr

僤鯓⒐⒋嵵緔 提交于 2021-01-21 11:18:24
问题 I'm re-implementing __getattribute__ for a class. I want to notice any incorrect (meaning failures are expected, of course) failures of providing attributes (because the __getattribute__ implementation turned out quite complex). For that I log a warning if my code was unable to find/provide the attribute just before raising an AttributeError . I'm aware: __getattribute__ implementations are encouraged to be as small as simple as possible. It is considered wrong for a __getattribute__

Detect if a __getattribute__ call was due to hasattr

二次信任 提交于 2021-01-21 11:18:04
问题 I'm re-implementing __getattribute__ for a class. I want to notice any incorrect (meaning failures are expected, of course) failures of providing attributes (because the __getattribute__ implementation turned out quite complex). For that I log a warning if my code was unable to find/provide the attribute just before raising an AttributeError . I'm aware: __getattribute__ implementations are encouraged to be as small as simple as possible. It is considered wrong for a __getattribute__

Detect if a __getattribute__ call was due to hasattr

ⅰ亾dé卋堺 提交于 2021-01-21 11:16:29
问题 I'm re-implementing __getattribute__ for a class. I want to notice any incorrect (meaning failures are expected, of course) failures of providing attributes (because the __getattribute__ implementation turned out quite complex). For that I log a warning if my code was unable to find/provide the attribute just before raising an AttributeError . I'm aware: __getattribute__ implementations are encouraged to be as small as simple as possible. It is considered wrong for a __getattribute__

Beautifulsoup and Soupstrainer for getting links dont work with hasattr, returning always true

♀尐吖头ヾ 提交于 2019-12-22 01:16:59
问题 i am using Beautifulsoup4 and Soupstrainer with Python 3.3 for getting all links from a webpage. The following is the important code-snippet: r = requests.get(adress, headers=headers) for link in BeautifulSoup(r.text, parse_only=SoupStrainer('a')): if hasattr(link, 'href'): I tested some webpages and it works very well but today when using adress = 'http://www.goldentigercasino.de/' I recognized that hasattr(link, 'href') always returns TRUE even when there is no such 'href' field, like in

Python's hasattr sometimes returns incorrect results

半城伤御伤魂 提交于 2019-12-07 01:47:23
问题 Why does hasattr say that the instance doesn't have a foo attribute? >>> class A(object): ... @property ... def foo(self): ... ErrorErrorError ... >>> a = A() >>> hasattr(a, 'foo') False I expected: >>> hasattr(a, 'foo') NameError: name 'ErrorErrorError' is not defined` 回答1: The python2 implementation of hasattr is fairly naive, it just tries to access that attribute and see whether it raises an exception or not. Unfortunately, this means that any unhandled exceptions inside properties will

Python's hasattr sometimes returns incorrect results

荒凉一梦 提交于 2019-12-05 08:19:23
Why does hasattr say that the instance doesn't have a foo attribute? >>> class A(object): ... @property ... def foo(self): ... ErrorErrorError ... >>> a = A() >>> hasattr(a, 'foo') False I expected: >>> hasattr(a, 'foo') NameError: name 'ErrorErrorError' is not defined` The python2 implementation of hasattr is fairly naive, it just tries to access that attribute and see whether it raises an exception or not. Unfortunately, this means that any unhandled exceptions inside properties will get swallowed, and errors in that code can get lost. To add insult to injury, when hasattr eats the exception

Beautifulsoup and Soupstrainer for getting links dont work with hasattr, returning always true

随声附和 提交于 2019-12-04 19:24:58
i am using Beautifulsoup4 and Soupstrainer with Python 3.3 for getting all links from a webpage. The following is the important code-snippet: r = requests.get(adress, headers=headers) for link in BeautifulSoup(r.text, parse_only=SoupStrainer('a')): if hasattr(link, 'href'): I tested some webpages and it works very well but today when using adress = 'http://www.goldentigercasino.de/' I recognized that hasattr(link, 'href') always returns TRUE even when there is no such 'href' field, like in the goldentigercasino.de example. Because of that im getting troubles for late using link['href'] because

Python's hasattr on list values of dictionaries always returns false?

我只是一个虾纸丫 提交于 2019-11-27 17:12:16
问题 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(info, key_string) print getattr(info, key_string, []) if hasattr(info, key_string): array = getattr(info, key_string, []) array.append(integer) info[key_string] = array print "current info:", info The first time this runs with integer = 1 : current info: {} False [] current info: {'foo': [1]} Running

Checking for member existence in Python

为君一笑 提交于 2019-11-27 08:50:36
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

hasattr() vs try-except block to deal with non-existent attributes

跟風遠走 提交于 2019-11-27 06:35:19
if hasattr(obj, 'attribute'): # do somthing vs try: # access obj.attribute except AttributeError, e: # deal with AttributeError Which should be preferred and why? hasattr internally and rapidly performs the same task as the try/except block: it's a very specific, optimized, one-task tool and thus should be preferred, when applicable, to the very general-purpose alternative. ZeD Any benches that illustrate difference in performance? timeit it's your friend $ python -mtimeit -s 'class C(object): a = 4 c = C()' 'hasattr(c, "nonexistent")' 1000000 loops, best of 3: 1.87 usec per loop $ python