[Python-Dev] Scoping [Patience, please]

lingwitt at gmail.com lingwitt at gmail.com
Wed Jan 31 15:15:38 CET 2007


This email is broken up into multiple sections:
	(1) Introduction
	(2) Problem
	(3) Probably Poor Solution
	(4) Tangent (Utter Tomfoolery)



#### Introduction ####

Hello,

This is my first post to the list,
and I apologize for 4 reasons:
	(1) I haven't been lurking for more than a few days.
	(2) I am by no means well versed in python.
	(3) I know nothing of language design.
	(4) This isn't a very good introduction.

Nonetheless, I have a habit of beginning with institutions
that are on the verge of  sweeping changes, and python
seems no different.

To curb the future troubles against which this unfortunate
fate of mine often forces me, I have been pouring over PEPs
(scanning really, because it's far past my bed time).

I came across the discussion of scoping, and while it seems
to be a very badly beaten and decaying horse, I ask that you'd
be kind enough to include me in this disgruntled discourse.



#### Problem ####

See http://www.python.org/dev/peps/pep-3104/

Consider the following:

 >>> x = 10
 >>> def a():
...     x = 3
...     def b():
...         global x
...         x = 4
...         print x
...
...     b()
...     print x
...
 >>> a()
4
3
 >>> print x
4

The keyword 'global' is only good for the module scope.



#### Probably Poor Solution ####

See http://www.python.org/dev/peps/pep-3104/

There seems to be a predilection towards limited
and/or general means of outer scope referencing,
such as
	(1) prefixing variables with dots.
	(2) scope override declarations.

However, I don't understand the virtue of this generality
(of course, I haven't thought much about it either, so please
lend me your expertise).

Most of the time, it would seem a person should know exactly which
outer variable he is after; he knows exactly which scope.

Fortunately, the scopes in question are always named.
Can't we just reference what's already there?

 >>> x = 10
 >>> def a():
...     x = 3
...     def b():
...         a.x = 4
...         print a.x
...
...     b()
...     print x
...
 >>> a()
4
4
 >>> print x
10

Of course, the module itself is unnamed, and it would
be completely consistent to use the single prefixed dot
to reference it:

 >>> x = 10
 >>> def a():
...     x = 3
...     def b():
...         a.x = 4
...         print a.x
...
...    .x = 5
...     b()
...     print x
...
 >>> a()
4
4
 >>> print x
5

However, I would think a placeholder is easier to spot:

 >>> x = 10
 >>> def a():
...     x = 3
...     def b():
...         a.x = 4
...         print a.x
...
...    @.x = 5
...     b()
...     print x
...
 >>> a()
4
4
 >>> print x
5

Of course, what happens if 'a' is a variable name?

 >>> x = 10
 >>> def a():
...     x = 3
...     def b():
...         a = 15
...         a.x = 4     #?
...         print a.x
...
...    @.x = 5
...     b()
...     print x
...

This would never happen in practice. Why bother considering it?
However, one could get around it with a fuller qualification:

 >>> x = 10
 >>> def a():
...     x = 3
...     def b():
...         a = 15
...         @.a.x = 4
...         print a.x
...
...    @.x = 5
...     b()
...     print x
...
 >>> a()
4
4
 >>> print x
5

In any case, those are just some thoughts off of the top of my  
(tired) head.
It seems to me that it unifies a lot of scoping ideas. everything can  
be accessed
with a qualified name.

Sorry if this is nonsensical.



#### Tangent (Utter Tomfoolery) ####

More interestingly, one sees (if one squints) the distinction between  
modules,
classes, and functions blur:

 >>> def new_b():
...     print "tee hee"
 >>> a.b = new_b()
 >>>a()
tee hee
3
 >>> print x
5

or perhaps:

 >>> # Interestingly, the following code is valid already
...
 >>> def Person(name, age, location):
...     def print_record():
...         print('The last person created had the name ' +  
Person.name_last)
...         print("However, I don't know any of the details")
...
...     def instance():
...         def print_record():
...             print('Name: ' + name)
...             print('age: ' + str(age))
...             print('location: ' + location)
...
...     if name == 'lingwitt':
...         name = age = location = 'classified'
...
...     Person.name_last = name
...
...     return instance
...
 >>>p1 = Person('spam', 80, 'eggsville')
 >>>Person.name_last
spam
 >>># Now the code becomes invalid
...
 >>>Person.print_record()
The last person created had the name spam
However, I don't know any of the details
 >>>p1.print_record()
name: spam
age: 80
location: eggsville



It would appear that modules, classes, and functions are scopes
with different magic associated with them.


#### Warning!!! This is an unlisted section! Sillyness Ensues ####

It would appear that a language could be built around scopes;
One could define a scope and then use that same scope as a template
for other scopes (actually, it sounds kind of like prototyping  
languages);
this seems to encompass the behavior of modules, classes, and functions.

Seems like all the "kinds" of scopes could be unified.

I'll stop here, because now I'm worried that I've made myself look  
silly.

Thanks for your time.

Yours truly,
Herr Witten

P.S.

Be gentle.


More information about the Python-Dev mailing list