Determining scope / context of a method call in python -


i write decorator python class method can determine if method called public context or private context. example, given following code

def public_check_decorator(f):     def wrapper(self):         if self.f `called publicly`:  # <-- how make line work correctly?             print 'called publicly'         else:             print 'called privately'         return f(self)     return wrapper  class c(object):     @public_check_decorator     def public_method(self):         pass      def calls_public_method(self):         self.public_method() 

runtime execution ideally this:

>>> c = c() >>> c.public_method() called publicly  >>> c.calls_public_method() called privately 

is there way in python? is, alter line

if self.f `called publicly`:  # <-- how make line work correctly? 

to give desired output?

some of seems trying swim against current of "python". appropriate?

do know double-unscore standard? makes methods "more private":

>>> class c(object): ...    def __hide_me(self): ...        return 11 ...    def public(self): ...        return self.__hide_me() ... >>> c = c() >>> c.__hide_me() traceback (most recent call last):   file "<stdin>", line 1, in <module> attributeerror: 'c' object has no attribute '__hide_me' >>> c.public() 11 >>> c._c__hide_me() 11 >>> 

is private enough? , using technique pythonic.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -