Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Ran into this face-first, just yesterday. I'm converting a rather old, rather big API (~400 endpoints) from Python 2.7 into 3, and apparently in Python 2, _hasattr(something, someattribute)_ just returns False if attribute access throws an exception! Specifically, if you have

  class A():
    @property
    def a(self): 
      raise Exception('go away')

  a = A()
 
then hasattr(a, 'a') will return False in Python 2.7 (and throw as expected in Python3)

A true WTF moment, the tests and the API have been slightly broken for years, without anyone noticing.



This is a rather unusual problem and I would call this design flawed. Defining useless methods that only exist to raise an exception is in my opinion a waste of space, both virtual and textual.


obv, this is just a minimal demo (i don't name my classes A!); in actual codebase, there's a complicated calculation that throws up under some circumstances




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: