Message118106
It's a well documented behavior.
Surely you ran across this link while researching your problem? http://docs.python.org/reference/executionmodel.html#naming-and-binding
The paragraph beginning "The following constructs bind names ..." describes why setattr does not create a binding, although I'll grant that it doesn't mention setattr by name. The paragraph beginning "If a name binding operation occurs anywhere within a code block ..." explains why the assignment affects code that occurs before the binding.
If you'd like to improve the docs, please suggest a patch.
Also note that you're not creating a local variable with setattr, you're creating a module level (sometimes called global) variable. Perhaps that's part of the misunderstanding? You might want to look into the global (on nonlocal) statement, unless there's some particular reason the name you're creating needs to be dynamically computed. |
|
Date |
User |
Action |
Args |
2010-10-07 12:31:16 | eric.smith | set | recipients:
+ eric.smith, docs@python, ssc |
2010-10-07 12:31:16 | eric.smith | set | messageid: <1286454676.64.0.0604361872436.issue10043@psf.upfronthosting.co.za> |
2010-10-07 12:31:14 | eric.smith | link | issue10043 messages |
2010-10-07 12:31:13 | eric.smith | create | |
|