Pointers to variables

Christian Tismer tismer at appliedbiometrics.com
Thu Apr 22 14:04:36 EDT 1999


Randall Hopper wrote:
> 
> 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?

There is simply no direct way to use references. You need
to use an object which itself has a reference.

One way to achive the wanted effect is to use setattr, while
paying some speed penalty, of course:

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

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




More information about the Python-list mailing list