Update locals()

holger krekel pyth at devel.trillke.net
Sat Apr 27 19:30:27 EDT 2002


On Sat, Apr 27, 2002 at 10:45:50PM +0000, Alex Martelli wrote:
> holger krekel wrote:
>         ...
> > Uff. Thanks for the pointer although it is dissappointing. I always
> > liked to assume that python does this decision at run time. Doing
> > it at compile time may have performance benefits but it prevents
> > some nice uses.
> 
> It only prevents some terrible hacks that Python is MUCH better
> off forbidding.  Python doesn't go out of its way to stop terrible
> hacks, but it's sure nice when a crucial optimization has such nice
> side effects. 
> As you've noticed, it's easy to dress up dictionary
> lookup by constant keys into attribute-access syntax -- so easy,
> that you and I came up with two totally different 3-liner classes
> for the same purpose.  

speaking of it, why is it possible (read: works) to say

class dictlookup(dict):
    __getattr__ = dict.get

but not

class dictlookup(dict):
    __getattr__ = dict.__getitem__

am i missing something? 

> why ever should the dirty, terrible hacks be allowed 

i would answer "for use with exec" but you already said that
this works. You are probably going to say that "exec" is the
next candidate for evil hacks. That may be true but it certainly
has its good uses and i appreciate that assigning to locals from
within "exec" is allowed. When you want to have e.g. templated 
parts of your program (see c++) this really helps. Or would
you go as far as to say that templating is not really needed
with python?

thanks,

    holger





More information about the Python-list mailing list