Re: yet another modifying locals() question

2009-09-14 Thread Ed Anuff
On Sep 13, 8:15 pm, Steven D'Aprano
ste...@remove.this.cybersource.com.au wrote:
 Metaclasses are left as an exercise for the reader.

The parent class has a metaclass, which is why I was trying this
approach instead, since it let me get at the class attributes before
the metaclass did.  Overriding the metaclass looked to be a much more
messy proposition, but I think I've got that working now.  Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


yet another modifying locals() question

2009-09-13 Thread Ed Anuff
I know that locals() is not supposed to be modifiable under most
circumstances, but I'm trying to solve a situation where I'm
dynamically generating some class attributes and it seemed to be the
best way, so I tried something out that seems to work but I'm not sure
that it's kosher:

 def f(l):
...l['b'] = 1
...
 class A:
...f(locals())
...
 A.b
1

In my code, I'm doing something quite a bit more complex than just
assigning a single attribute, but this is the simplest use case
example.

Is there a reason why this works and is it safe to rely on it or is
there a better approach?  BTW, this works in a program too, it's not
just an artifact of the command line interpreter globals() = locals()
thing.

Thanks

Ed

-- 
http://mail.python.org/mailman/listinfo/python-list