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.
lingwitt@gmail.com wrote:
#### 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
After falling into bed, I realized that everything after this point contradictions with my earlier usage of qualified names. Upon further investigation, it seems what I have suggested above conflicts with the use of func_dict.
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") ...
On 31/01/07, lingwitt@gmail.com
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
There is further research you need to do, as this option has been discussed before. I'm not sure if it's in the PEP, but it has certainly come up on the mailing list - you should check the archives for more information. Basically, you can't use "a.x" as the function a has not yet been created - you are still executing the "def". So you don't have access to the function object's attributes in the way you need. (The technical details are beyond me, and that explanation probably shows it, but I hope you get the gist :-))
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
Yuk.
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
Double yuk.
Be gentle.
Sorry, I could probably have been more gentle :-) Seriously, thanks for your interest, and don't be put off, but if you pick up on a long-standing proposal like this, you really do need to do a lot of research. Many of the obvious, a lot of the not-so-obvious, and even some of the downright stupid options will have already been discussed, and you can't always guarantee that everything gets captured in the PEP. Checking the list archives is always a good idea. If you want to get involved without needing to do quite so much pre-work, joining in on one of the current threads is often a good way to start. Hope this helps, Paul.
participants (2)
-
lingwitt@gmail.com
-
Paul Moore