Need help with Python scoping rules
no.email at please.post
Wed Aug 26 22:11:02 CEST 2009
In <mailman.459.1251313091.2854.python-list at python.org> Ethan Furman <ethan at stoneleaf.us> writes:
>Going back through the archives I found Arnaud's post with this decorator:
> def boundf(*args, **kwargs):
> return f(boundf, *args, **kwargs)
> return boundf
>If you use it on your fact function like so...
> def fact(recurse, n) # recurse can be any name you like
> if n < 2:
> return 1
> return n * recurse(n-1)
> _classvar = fact(5)
> del fact # no longer needed, and won't work
> # once class is created
>This should do as you want.
Thanks, this is instructive.
>As a side note, if you're going to bother asking questions on this list,
>you really should try to understand the answers.
I think I understand the answers well enough. What I *really*
don't understand is why this particular "feature" of Python (i.e.
that functions defined within a class statement are forbidden from
"seeing" other identifiers defined within the class statement) is
generally considered to be perfectly OK. IMO it's a bizarre,
inexplicable blindspot (which, among other things, gives rise to
a certain worry about what other similar craziness lurks under
Python's image of rationality). I have never seen even a half-hearted
justification, from a language design point of view, for why this
particular "feature" is worth having. Maybe some day the BDFL will
deign to give one.
More information about the Python-list