Prev: Python DXF import library?
Next: "ssl" module doesn't validate that domain of certificate iscorrect
From: Andreas Löscher on 21 Apr 2010 06:03
You can do something like this:
>>> class A(): pass
.... """) in inst.__dict__
This executes the Statement in the exec function and uses inst.__dict__
as namespace. But be aware, that this is not recommended. If you mess
with __dict__, you won't be able to replace it with some logic
(parameter) if you need to do something more than setting a variable.
From: Ryan Kelly on 21 Apr 2010 18:33
On Tue, 2010-04-20 at 14:43 +0100, Alan Harris-Reid wrote:
> During my Python (3.1) programming I often find myself having to repeat
> code such as...
> class1.attr1 = 1
> class1.attr2 = 2
> class1.attr3 = 3
> class1.attr4 = 4
> Is there any way to achieve the same result without having to repeat the
> class1 prefix? Before Python my previous main language was Visual
> Foxpro, which had the syntax...
> with class1
> .attr1 = 1
> .attr2 = 2
> .attr3 = 3
> .attr4 = 4
> Is there any equivalent to this in Python?
Please don't take this as in invitation to disregard the excellent
advice already received in this thread - I just want to point out that
python can usually be bent to your will. Observe:
from withhacks import namespace
attr1 = 1
attr2 = 2
This will do pretty much what you get from the "with" statement in
But don't use this in any real code. Seriously, don't even think about
it. You don't want to know the kind of abuses that go on under the
covers to make this kind of syntax hacking work...
http://www.rfk.id.au | This message is digitally signed. Please visit
ryan(a)rfk.id.au | http://www.rfk.id.au/ramblings/gpg/ for details