Apr-14-2020, 11:39 PM
(Apr-14-2020, 07:49 PM)buran Wrote:class Foo: def spam(self): pass try: check = hasattr(Foo, 'eggs') except AttributeError: check = False print(check) print(hasattr(Foo, 'spam'))
I was misled by your AttributeError...
you don't need try/except
class Foo: def spam(self): pass check = hasattr(Foo, 'eggs') print(check) check = hasattr(Foo, 'spam') print(check)As a side note about using try/except - try should be before the line that may raise error, not after that.
try: print(1/0) except ZeroDivisionError: print('Cannot divide by 0')
I redid the code assigning the egg value in a variable and it generates an error instead of false?
class Foo: def spam(self): pass v_method = "egg" check = hasattr(Foo, v_method) print(check)