[Python-Dev] Scoping [Patience, please]
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
Re: [Python-Dev] Scoping [Patience, please]
[EMAIL PROTECTED] 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") > ... ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] Happy Birthday, Guido!
Thanks again for giving me something fun to do with my life. ;-) -- Aahz ([EMAIL PROTECTED]) <*> http://www.pythoncraft.com/ "I disrespectfully agree." --SJM ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Happy Birthday, Guido!
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Jan 31, 2007, at 10:11 AM, Aahz wrote: > Thanks again for giving me something fun to do with my life. ;-) Here, here! - -Barry -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.5 (Darwin) iQCVAwUBRcC0nnEjvBPtnXfVAQI2+AP/QcOBBiGI/zDreuFU2bjq0MNH8qIocHnA y2XkoaXFDNYlOO6Uj39kttFl8HecMmyTyLvcyMTFBDxxN0xxQBpOPBkzFe6D44HQ x8R/VsaSY8wB3G4NZyYOoBjJMPPTN1x6U3lg+jzHkuRE4SSFYZmoWuvUGK8hdEQ9 uDYqy/TlbT8= =JnWB -END PGP SIGNATURE- ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Scoping [Patience, please]
On 31/01/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > 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. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Happy Birthday, Guido!
Thank you guys! Now I want something fun to do with my life again too! :-) On 1/31/07, Barry Warsaw <[EMAIL PROTECTED]> wrote: > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > On Jan 31, 2007, at 10:11 AM, Aahz wrote: > > > Thanks again for giving me something fun to do with my life. ;-) > > Here, here! > - -Barry > > -BEGIN PGP SIGNATURE- > Version: GnuPG v1.4.5 (Darwin) > > iQCVAwUBRcC0nnEjvBPtnXfVAQI2+AP/QcOBBiGI/zDreuFU2bjq0MNH8qIocHnA > y2XkoaXFDNYlOO6Uj39kttFl8HecMmyTyLvcyMTFBDxxN0xxQBpOPBkzFe6D44HQ > x8R/VsaSY8wB3G4NZyYOoBjJMPPTN1x6U3lg+jzHkuRE4SSFYZmoWuvUGK8hdEQ9 > uDYqy/TlbT8= > =JnWB > -END PGP SIGNATURE- > ___ > Python-Dev mailing list > Python-Dev@python.org > http://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > http://mail.python.org/mailman/options/python-dev/guido%40python.org > -- --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Happy Birthday, Guido!
aahz> Thanks again for giving me something fun to do with my life. ;-) Not to mention a very good way to earn a living. ;-) Skip ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Happy Birthday, Guido!
"Guido van Rossum" <[EMAIL PROTECTED]> wrote: > Thank you guys! Now I want something fun to do with my life again too! :-) You could hand BDFL responsibilities off onto perhaps Tim, Raymond, or Martin for a few months and try to convince Google to open an office in Hawaii. Pick up one of those Toughbook laptops, and you could be coding on the beach all year round. Need to wait for some unittests to run? Catch a wave or two. Feeling a little stressed? Have a mimosa. Getting too much sun? Sit in the shade. The possibilities are endless. :) - Josiah ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Happy Birthday, Guido!
Barry Warsaw wrote: > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > On Jan 31, 2007, at 10:11 AM, Aahz wrote: > >> Thanks again for giving me something fun to do with my life. ;-) > > Here, here! Where, where? regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://del.icio.us/steve.holden Blog of Note: http://holdenweb.blogspot.com See you at PyCon? http://us.pycon.org/TX2007 ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Happy Birthday, Guido!
On Wed, Jan 31, 2007, Steve Holden wrote: > Barry Warsaw wrote: >>On Jan 31, 2007, at 10:11 AM, Aahz wrote: >>> >>>Thanks again for giving me something fun to do with my life. ;-) >> >>Here, here! > > Where, where? Therewolf -- Aahz ([EMAIL PROTECTED]) <*> http://www.pythoncraft.com/ "I disrespectfully agree." --SJM ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] Python's C interface for types
Nick Maclaren wrote: > Martin v. Löwis wrote: >> It may be a bit problematic to implement, but I think a clean >> specification is possible. If a and b are numbers, and a==b, >> then hash(a)==hash(b). You don't even need that much complication. If a==b, then hash(a) == hash(b) If you have to break this, then at least one (preferably both) of (a,b) must be unhashable, so that it won't get used as a dict key. Hashing is only ever meaningful as a shortcut to quickly show that two objects are *not* equal. >> I'm not sure whether "approximately 5.0" >> equals 5 or not: if it does, it should hash the same as 5, >> if it doesn't, it may or may not hash the same (whatever is >> easier to implement). agreed. >> For 0: hash(+0.0)==hash(-0.0)==hash(0)=hash(0L)=0 > Unfortunately, that assumes that equality is transitive. No, but the (transitively closed set of equivalent objects) must have the same hash. If >>> myfloat(5.0) != 5.0 True then you could just return 47 as the hash. It might not be terribly efficient, but it would work. If >>> myfloat_exact(5.0) == 5.0 == myfloat_approx(5.0) != myfloat_exact(5.0) then at least one of (myfloat_exact, myfloat_approx) needs to be unhashable, so that it can't be used as a dictionary key. > let us say that I am implementing a special function and want to > distinguish -0.0 and +0.0. Why can't I use a dictionary? Because they are equal. They aren't identical, but they are equal. >>>> a = float("+0.0") >>>> b = float("-0.0") >>>> print a, b >0.0 -0.0 With the standard windows distribution, I get just 0.0 0.0 > No, I don't have an answer. You are damned if you do, and damned > if you don't. It is an insoluble problem, and CURRENTLY doesn't > justify two hashing mechanisms (i.e. ANY difference and EQUALITY > difference). You want something in between "__eq__" and "is". (a.identical(b) ?) Hashing is weaker than either. >>> hash ("JimJ") == hash (2010274390) True -jJ ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] dict(keys, values)
Perhaps this has been brought up in the past but I couldn't find it in the archives: far too often I use the idiom dict(zip(keys,values)), or the same with izip. How does letting dict take two positional arguments sound ? Pros: - Pretty obvious semantics, no mental overhead to learn and remember it. - More concise (especially if one imports itertools just to use izip). - At least as efficient as the current alternatives. - Backwards compatible. Cons: - ?? George ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com