Need help with Python scoping rules

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Aug 26 11:40:35 EDT 2009


On Wed, 26 Aug 2009 13:57:23 +0000, kj wrote:

> In <jeqdncAMUYvTrwjXnZ2dnUVZ8ludnZ2d at bt.com> "Martin P. Hellwig"
> <martin.hellwig at dcuktec.org> writes:
> 
>>kj wrote:
>><cut>
>>> First, one of the goals of OO is encapsulation, not only at the level
>>> of instances, but also at the level of classes.
>>Who says?
> 
> Python itself: it already offers a limited form of class encapsulation
> (e.g. class variables).

An int variable is an int. A string variable is a string. A bool variable 
is a bool. A class variable is a class.

Perhaps you mean a class attribute?


> It would be nice if it went all the way and
> gave classes their own bona fide scope.

Classes have their own bona fide scope. It just doesn't work the way you 
expect it to.


> (But I hasten to add: I *still*
> don't understand the Python scope model, and not for lack of trying. 
> I've only succeeded in finding fragments of this model explained here
> and there, like pottery shards: a bit lost in a tutorial, or some
> comment in a newsgroup thread, etc.  Maybe the full, authoritative
> documentation of Python's scope model got lost somewhere, and will be
> found by archaeologists in the 25th century...)

It's all in the Fine Manual:

http://docs.python.org/reference/executionmodel.html


 
>>Anyway, you could be right (I am not capable to judge it) and Python
>>should change on this issue but from what I gathered, Pythons OO is
>>inspired by the following:
>>- Don't repeat yourself
>>- Containing code into logical units makes it easier to understand and
>>maintain.
> 
> ...except, apparently, when that code is a recursive function, in which
> case one's out of luck.

Incorrect. As I showed in my previous post, your problem has nothing to 
do with recursion.


>>> Second, my example shows that Python puts some peculiar restrictions
>>> on recursion.  Recursion!  One of the central concepts in the theory
>>> of functions!
>><cut>
>>It is also one of the best ways to shoot yourself in the foot...
> 
> If recursion is so evil, and Python so intent in saving us from shooting
> ourselves in the foot, why does it allow recursion at all?

Recursion isn't evil, and Python isn't intent on preventing foot-shooting.



-- 
Steven



More information about the Python-list mailing list