Advertisement
Guest User

Accidental Python class method override

a guest
Aug 22nd, 2018
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. >>> class A(object):
  2.         def __init__(self, **kwargs):
  3.             for key, value in kwargs.items():
  4.                 setattr(self, key, value)
  5.         def method(self):
  6.             print(self.__dict__)
  7.        
  8. >>> a = A(x=1, y=2)
  9. >>> a.method()
  10. {'a': 1, 'b': 2}
  11.  
  12. >>> a = A(x=1, y=2, method="blah")
  13. >>> a.method()
  14. Traceback (most recent call last):
  15.   File "/python3.5/site-packages/IPython/core/interactiveshell.py", line 281, in run_code
  16.     exec(code_obj, self.user_global_ns, self.user_ns)
  17.   File "<ipython-input-29-76ebeb9909f4>", line 1, in <module>
  18.     a.method()
  19. TypeError: 'str' object is not callable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement