You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.9 KiB

From: dfan at (Dan Schmidt)
Date: 22 Apr 1999 13:30:53 -0400
Subject: Pointers to variables
References: <>
Message-ID: <>
Content-Length: 1652
X-UID: 223
Randall Hopper <aa8vb at> writes:
| This doesn't work:
| for ( var, str ) in [( self.min, 'min_units' ),
| ( self.max, 'max_units' )]:
| if cnf.has_key( str ):
| var = cnf[ str ]
| del cnf[ str ]
| It doesn't assign values to self.min, self.max (both integers). The
| values of these variables are inserted into the tuples and not
| references to the variables themselves, which is the problem.
| How can I cause a reference to the variables to be stored in the
| tuples instead of their values?
Here's how I would do it:
for ( varname, str ) in [( 'min', 'min_units' ),
( 'max', 'max_units' )]:
if cnf.has_key( str ):
setattr (self, varname, cnf[ str ])
del cnf[ str ]
I don't think this is really any slower than what you wanted to do,
since Python was doing namespace lookups on 'min' and 'max' before
If min and max were mutable objects, a variation of your approach
would probably work, if there were a method to change them in-place
(e.g., "var.set_me (cnf[str])"). "var = cnf[str]" is never going to
do what you want, since it just makes the name 'var' point to a
different object (that's what assignment means in Python).
I've just been using Python for a week, so take my answer with a grain
of salt...
Dan Schmidt -> dfan at, dfan at
Honest Bob & the
Factory-to-Dealer Incentives ->
Gamelan Galak Tika ->