Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31236
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <cameron@cskk.homeip.net> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'none,': 0.05; 'none:': 0.05; 'sys': 0.05; '21,': 0.07; 'decorator': 0.07; 'sanity': 0.07; 'python': 0.09; 'cached': 0.09; 'macosx': 0.09; 'method:': 0.09; 'referenced': 0.09; 'wrong,': 0.09; 'def': 0.10; 'value.': 0.15; '2.7.3': 0.16; 'caches': 0.16; 'closures': 0.16; 'fetches': 0.16; 'fine.': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:202.125.174': 0.16; 'received:202.125.174.133': 0.16; 'received:boardofstudies.nsw.edu.au': 0.16; 'received:cskk.homeip.net': 0.16; 'received:edu.au': 0.16; 'received:harvey.boardofstudies.nsw.edu.au': 0.16; 'received:homeip.net': 0.16; 'received:nsw.edu.au': 0.16; 'reload': 0.16; 'rewriting': 0.16; 'seconds.': 0.16; 'simpson': 0.16; 'stripped': 0.16; 'useless.': 0.16; 'variable': 0.20; 'changes': 0.20; 'skip:" 30': 0.20; 'import': 0.21; 'assignment': 0.22; 'bug?': 0.22; 'example': 0.23; 'work.': 0.23; 'this:': 0.23; "i've": 0.23; 'pass': 0.25; 'header:User-Agent:1': 0.26; 'appear': 0.26; 'looks': 0.26; 'wrote': 0.26; 'skip:" 20': 0.26; 'skip:m 30': 0.26; '(most': 0.27; 'i.e.': 0.27; 'skip:> 10': 0.27; 'skip:@ 10': 0.27; 'all.': 0.28; 'trouble': 0.28; 'class': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'checks': 0.30; 'function': 0.30; 'code': 0.31; 'gets': 0.32; 'file': 0.32; 'print': 0.32; 'defining': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'needed': 0.35; 'eric': 0.35; 'so,': 0.35; 'doing': 0.35; "won't": 0.35; 'next': 0.35; 'explain': 0.36; 'but': 0.36; 'received:au': 0.36; 'subject:with': 0.36; 'charset:us-ascii': 0.36; 'does': 0.37; 'why': 0.37; 'quite': 0.37; 'well.': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'skip:" 10': 0.40; 'content-disposition:inline': 0.60; 'skip:u 10': 0.60; 'back': 0.62; 'mountain': 0.62; 'subject:...': 0.63; 'times': 0.63; 'more': 0.63; 'here': 0.65; 'background:': 0.65; 'realise': 0.65; 'as:': 0.75; 'watches': 0.84; 'same,': 0.91 |
| Date | Sun, 14 Oct 2012 14:59:24 +1100 |
| From | Cameron Simpson <cs@zip.com.au> |
| To | python-list@python.org |
| Subject | trouble with nested closures: one of my variables is missing... |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Disposition | inline |
| User-Agent | Mutt/1.5.21 (2010-09-15) |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2161.1350187169.27098.python-list@python.org> (permalink) |
| Lines | 113 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1350187169 news.xs4all.nl 6856 [2001:888:2000:d::a6]:58722 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:31236 |
Show key headers only | View raw
I'm having some trouble with closures when defining a decorator.
TL;DR:
I have a function that makes a decorator, and only some of the names
from an outer scope appear in the inner closure's locals().
And I do not understand why at all.
Let me explain...
Environment: python 2.7.3 on MacOSX Mountain Lion from MacPorts.
Background: I have a decorator called "file_property" that watches a
file for changes and reloads the file at need. Otherwise it returns a
cached value. It has a lock and some sanity checks and works quite well.
Example method:
class myclass(object):
@file_property
def rules(self):
with open(self._rules_path) as rfp:
R = parse(rfp)
return R
C = myclass()
and using C.rules fetches parses the rule file as needed and caches the
value until the file next changes.
I naively wrote file_property with a bunch of default parameters:
def file_property(func, attr_name=None, unset_object=None, poll_rate=1):
and because I never overrode these failed to realise they're useless. Fine.
So, I'm rewriting file_property like this:
def file_property(func):
return make_file_property()(func)
i.e. it makes a "vanilla" file_property using the default internals.
Now I have make_file_property with the default parameters:
def make_file_property(attr_name=None, unset_object=None, poll_rate=1):
which I might use as:
class myclass(object):
@make_file_property(poll_rate=3)
def rules(self):
with open(self._rules_path) as rfp:
R = parse(rfp)
return R
The inner function is the same, but it won't reload the file more often
that once every 3 seconds.
However, I can't make my make_file_property function work. I've stripped
the code down and it does this:
[hg/css-mailfiler]fleet*1> python foo.py
make_file_property(attr_name=None, unset_object=None, poll_rate=1): locals()={'attr_name': None, 'poll_rate': 1, 'unset_object': None}
made_file_property(func=<function f at 0x10408b0c8>): locals()={'func': <function f at 0x10408b0c8>, 'unset_object': None}
Traceback (most recent call last):
File "foo.py", line 21, in <module>
def f(self, foo=1):
File "foo.py", line 4, in file_property
return make_file_property()(func)
File "foo.py", line 10, in made_file_property
if attr_name is None:
UnboundLocalError: local variable 'attr_name' referenced before assignment
Observe above that 'unset_object' is in locals(), but not 'attr_name'.
This surprises me.
The stripped back code (missing the internals of the file property
watcher) looks like this:
import sys
def file_property(func):
return make_file_property()(func)
def make_file_property(attr_name=None, unset_object=None, poll_rate=1):
print >>sys.stderr, "make_file_property(attr_name=%r, unset_object=%r, poll_rate=%r): locals()=%r" % (attr_name, unset_object, poll_rate,locals())
def made_file_property(func):
print >>sys.stderr, "made_file_property(func=%r): locals()=%r" % (func, locals())
if attr_name is None:
attr_name = '_' + func.__name__
lock_name = attr_name + '_lock'
def getprop(self):
with getattr(self, lock_name):
# innards removed here
pass
return getattr(self, attr_name, unset_object)
return property(getprop)
return made_file_property
@file_property
def f(self, foo=1):
print "foo=%r" % (foo,)
@make_file_property(attr_name="_blah")
def f2(self, foo=2):
print "foo=%r" % (foo,)
Can someone explain what I'm doing wrong, or tell me this is a python
bug?
--
Cameron Simpson <cs@zip.com.au>
Bolts get me through times of no courage better than courage gets me
through times of no bolts!
- Eric Hirst <eric@u.washington.edu>
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
trouble with nested closures: one of my variables is missing... Cameron Simpson <cs@zip.com.au> - 2012-10-14 14:59 +1100
csiph-web