Advertisement
roman_gemini

Private Method

Jan 21st, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. import inspect
  2.  
  3.  
  4. def private(method):
  5.     def func(self, *args, **kwargs):
  6.         print("Private method call")
  7.         outer_frame = inspect.stack()[1][0]
  8.         if 'self' not in outer_frame.f_locals or outer_frame.f_locals['self'] is not args[0]:
  9.             raise Exception('Call to a private method outside!!!')
  10.         method(self)
  11.     return func
  12.  
  13.  
  14. class Some:
  15.     def foo(self):
  16.         print("Foo!")
  17.  
  18.     def bar(self):
  19.         print("Bar!")
  20.         self.baz(self)
  21.  
  22.     @private
  23.     def baz(self):
  24.         print("Baz!")
  25.  
  26.  
  27. some = Some()
  28.  
  29. some.foo()
  30. some.bar()
  31. some.baz()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement