decorators and mangled names for "private" methods

Tim Chase python.list at tim.thechases.com
Sat Oct 26 11:46:24 EDT 2013


On 2013-10-25 22:01, Peter Otten wrote:
> > from functools import wraps
> > class require_keys:
> >   def __init__(self, *keys):
> >     self.keys = keys
> >   def __call__(decorator_self, fn):
> >     @wraps(fn)
> >     def result_fn(method_self, *args, **kwargs):
> >       # import pdb; pdb.set_trace()
> >       req = method_self.__private()  
> 
> The above __private literal is in the (statically determined)

I guess the static determination is/was what threw me.  I'd have
figured that Python wouldn't care what method-name/attribute I looked
up, until such time as the code was actually executed, at which
point it would be evaluated in the context of Foo, not the
require_keys decorator.

For now, I'll just smash the code a bit and un-privatize the
functions (it's hand-me-down code from a C/C++ programmer, so there
are a lot of C++isms in the source).

-tkc






More information about the Python-list mailing list