Assignments and globals

echuck3 at my-deja.com echuck3 at my-deja.com
Fri Aug 18 16:01:58 EDT 2000


My intent was that the settings on the left was a local and the one on
the right was a global. My thought process was that there's no need to
consider what's on the left until the right is evaluated, so I should
be able to read the global and set the local.

In other words, "settings = settings['x']" could break down to:

* Eval the right hand expression
  * Look up 'settings'; find as a global.
  * Send __getitem__('x')
* Handle left hand of assignment
  * 'settings' was not previously named by a 'global' statement
  * Make a new local called 'settings' and assign the value

But as you pointed out, Python determines ahead of time how to treat
the name and does so uniformily. I checked the FAQ links from other
replies and even the FAQ states that it can be confusing and gives a
reasonable example:

http://www.faqts.com/knowledge-base/view.phtml/aid/2902/fid/241


In any case, now I know: If I read a global 'x', I cannot assign to
local 'x'.

I'll add that to my "arcane list".


Here's another good one: Copy a dictionary with the copy() method.
Sounds good. Copy a list with...[:]? While I certainly support [:] as a
valid technique that meshes with slicing, it wouldn't hurt to have a
copy() method for copying.

Python's pretty clean, especially in light of C++ and Perl, but I still
see some oddities. I wonder in what ways Py3K will address these, if at
all.


-Chuck


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list