I simply didn't believe it at first, but sure enough, in python 2.7: >>> class Bomb(object): ... def __getattr__(self, id): ... assert False ... >>> bomb = Bomb() >>> bomb.foo Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in __getattr__ AssertionError >>> hasattr(bomb, 'foo') False
Thankfully Python 3 fixes this. One more reason to switch, I guess. On Thursday, September 4, 2014 12:04:36 PM UTC-4, Collin Anderson wrote: > > believe it or not, hasattr is the same as: > > try: > getattr(obj, name) > return True > except: # yikes! naked except! > return False > > https://docs.python.org/2/library/functions.html#hasattr > So, excepting AttributeError will at least narrow down the error slightly, > rather than hasattr accepting _any_ exception. > > > On Thu, Sep 4, 2014 at 4:29 AM, Michael Jones <m.pric...@gmail.com > <javascript:>> wrote: > >> Hi, >> >> Thanks for the reply, but I'm not sure I follow. I was hoping that the >> following might work: >> >> if hasattr(sup_cls, 'media'): >> base = sup_cls.media >> else: >> base = Media() >> >> Would that swallow additional exceptions? >> >> Cheers, >> Michael >> >> >> On Wednesday, September 3, 2014 10:45:22 PM UTC+1, Collin Anderson wrote: >>> >>> unfortunately hasattr eats even more exceptions than AttributeError. >>> >> -- >> You received this message because you are subscribed to a topic in the >> Google Groups "Django users" group. >> To unsubscribe from this topic, visit >> https://groups.google.com/d/topic/django-users/K26vVvKWnTs/unsubscribe. >> To unsubscribe from this group and all its topics, send an email to >> django-users...@googlegroups.com <javascript:>. >> To post to this group, send email to django...@googlegroups.com >> <javascript:>. >> Visit this group at http://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/9db016be-bc63-44b4-934c-7b2c64ae9863%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-users/9db016be-bc63-44b4-934c-7b2c64ae9863%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8539f5e7-2fb9-4af4-8ccf-436ae0f847d3%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.