Pointers to variables

Evan Simpson evan at tokenexchange.com
Thu Apr 22 13:52:51 EDT 1999


Randall Hopper wrote in message <19990422121403.A279051 at vislab.epa.gov>...
[snip]
> How can I cause a reference to the variables to be stored in the tuples
instead of their values?

You can't, or at least not directly.  What you *can* do, in the case you
provided, is something like this:

for ( attrname, k ) in [( 'min', 'min_units' ), ( 'max', 'max_units' )]:
    if cnf.has_key( k ):
        setattr(self, attrname, cnf[ str ])
        del cnf[ str ]

Python simply doesn't *have* variables in the sense you're thinking of, with
the exception of function-local variables.  Instead it has namespaces, such
as modules, classes, and instances.  These can be usefully be thought of as
dictionaries with efficient string-only keys, and can be manipulated as such
using getattr, setattr, delattr, etc.

Function-local-variables-are-a-whole-nother-alien-world-ly yrs,
Evan Simpson






More information about the Python-list mailing list