56 lines
1.9 KiB
Plaintext
56 lines
1.9 KiB
Plaintext
|
From: dfan at harmonixmusic.com (Dan Schmidt)
|
||
|
Date: 22 Apr 1999 13:30:53 -0400
|
||
|
Subject: Pointers to variables
|
||
|
References: <19990422121403.A279051@vislab.epa.gov>
|
||
|
Message-ID: <wkk8v4ef5e.fsf@turangalila.harmonixmusic.com>
|
||
|
Content-Length: 1652
|
||
|
X-UID: 223
|
||
|
|
||
|
Randall Hopper <aa8vb at vislab.epa.gov> 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
|
||
|
anyway.
|
||
|
|
||
|
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 harmonixmusic.com, dfan at alum.mit.edu
|
||
|
Honest Bob & the http://www2.thecia.net/users/dfan/
|
||
|
Factory-to-Dealer Incentives -> http://www2.thecia.net/users/dfan/hbob/
|
||
|
Gamelan Galak Tika -> http://web.mit.edu/galak-tika/www/
|
||
|
|
||
|
|
||
|
|
||
|
|