Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57553
| Date | 2013-10-25 14:44 -0500 |
|---|---|
| From | Tim Chase <python.list@tim.thechases.com> |
| Subject | decorators and mangled names for "private" methods |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1539.1382730168.18130.python-list@python.org> (permalink) |
Given the following example 2.7 code:
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()
for key in decorator_self.keys:
if key not in req:
raise ValueError("Missing [%s] parameter" % key)
return fn(method_self, *args, **kwargs)
return result_fn
class Foo(object):
def __init__(self, *params):
self.params = params
self.__private = params * 2
def __private(self, *args, **kwargs):
return self.__private
@require_keys("hello", "world")
def action(self):
print self.params
f1 = Foo("hello", "world")
f1.action()
f2 = Foo("world")
f2.action()
I'm surprised to get the exception:
Traceback (most recent call last):
File "dec_examp.py", line 28, in <module>
f1.action()
File "dec_examp.py", line 10, in result_fn
req = method_self.__private()
AttributeError: 'Foo' object has no attribute '_require_keys__private'
For some reason, it's looking for "_require_keys__private" (which
obviously doesn't exist) instead of "_Foo__private" which exists
and would be what I expect.
What am I missing here? Why is the decorator class finding the wrong
private-scope?
Thanks,
-tkc
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
decorators and mangled names for "private" methods Tim Chase <python.list@tim.thechases.com> - 2013-10-25 14:44 -0500
csiph-web