[Tutor] loops to assign variables

Dave Kuhlman dkuhlman at rexx.com
Sat Jul 22 19:22:30 CEST 2006


On Sat, Jul 22, 2006 at 11:37:38AM -0400, Python wrote:
> On Sat, 2006-07-22 at 17:18 +0200, Karl Pflästerer wrote:
> > On 22 Jul 2006, python at venix.com wrote:
> > 
> > >
> > > On Sat, 2006-07-22 at 14:11 +0100, John CORRY wrote:
> > >> Hi,
> > >>  
> > >> I am refactoring my code.  I am trying to reduce the amount of lines
> > >> by using more loops.  I tend to use copy and paste a lot instead of
> > >> writing a loop to do the work.
> > >>  
> > >> For example, I have 30 textentry boxes numbered from entry20 to
> > >> entry50.
> > >> I have used the following code to assign the entryboxes to a local
> > >> name.
> > >>  
> > >> text20 = self.wTree.get_widget("entry20")
> > >> text21 = self.wTree.get_widget("entry21")
> > >>  
> > >> I have had a go at writing a loop for the above 30 textentry boxes.
> > >> It is below, but it does not work.  
> > >>  
> > >> for x in range(20,51):
> > >>             ent = "entry%s" % (str(x))
> > >>                    
> > >>             text_x = self.wTree.get_widget(ent)
> > >>  
> > >> Is it possible to do what I want it to do?  
> > >
> > > NO.  You are looking to create local variables "on the fly".  But there
> > > is a simple solution that accomplishes what you really want.
> > 
> > The "no" is not absolutely right IMO.  He could write directly in the
> > dictionary he gets when he calls locals() (but I think you're right in
> > saying that this is only seldom a good idea).
> 
> Well the reference documentation says:
> 
> locals(
> )
>         Update and return a dictionary representing the current local
>         symbol table. Warning: The contents of this dictionary should
>         not be modified; changes may not affect the values of local
>         variables used by the interpreter.
> 
> I believe the compiler thinks it knows of all the local variables, so I
> think assignment to locals() is likely to lead to grief even it appears
> to work in simple test cases.
> 

Lloyd is spot-on in telling you to avoid creating variables from
strings.  There almost always is a better way, which members of
this list seem to have to repeat once each week.

But, (and I'm going off-topic here and possibly into the ditch
along the side of the road) ..., locals() returns a dictionary.
It's a dictionary like any other dictionary.  And, it is that
dictionary which Python uses to check for the existence of a
variable *at runtime*.  Yes. That's right.  Python actually does a
dictionary look-up for each variable at runtime.  This is *not* a
flaw; it is part of the object model and execution model of
Python.  And, that's why compile-time (static) type checking in
Python is either impossible or would require type inference.

And, also, that's why the following statements all have exactly
the same effect:

    total = 5
    locals()['total'] = 5
    exec('total = 5')

But, again, as Lloyd said, don't do that.  (1) Use a dictionary of
your own and do a look up.  Or, (2) implement a class that does a
lookup.  Or, (3) use one of the other suggestion made on this
list.

Dave



-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list