How do I do this? (eval() on the left hand side)

Carl Banks imbosol at aerojockey.com
Fri Dec 10 23:25:39 CET 2004


> From my point of view, they're basically identical, and
> although I find Carl's approach slightly less explicit
> and harder to read (mainly the uncommon __import__ call,
> but it's not a big deal), I can't see why either of them
> would be considered evil.

Of course, when I said evil, I didn't mean evil, I meant Evil(tm).  I
suspect Nick meant so as well.

Personally, I just think __import__(__name__) is a bit more honest than
globals() for a simple reason.

What are you doing?  You're changing module attributes.  Well, here's
the module object.  You're using setattr on the module object to set
module attributes.

What are you doing?  You're changing module attributes.  Well, here's
the module's dict.  You're using dictionary access on the modules dict
to set modules attributes.

It's a bit more honest to set module attributes using setattr than dict
access, I would say.  That's why I prefer it.  I admit, it's pretty
weak.  (But then again, so is __import__ being uncommon. :)  It's not a
big deal which you choose, as you say.  Although I don't use globals()
in this way, I do modify object dicts to get the effect of changing
object attributes from time to time (mostly to take advantage of dict
methods, which I could see being a reason to use globals() as well).
-- 
CARL BANKS




More information about the Python-list mailing list