Re: __dict__ is neato torpedo!

2011-06-12 Thread Hans Mulder
On 12/06/11 10:47:01, Steven D'Aprano wrote: On Sat, 11 Jun 2011 21:28:40 -0500, Andrew Berg wrote: On 2011.06.11 09:13 PM, Steven D'Aprano wrote: A second, more subtle risk: not all objects have a __dict__. But if you obey the rule about never updating from arbitrary objects you don't know,

Re: __dict__ is neato torpedo!

2011-06-12 Thread Tim Chase
On 06/11/2011 08:32 PM, Andrew Berg wrote: I'm pretty happy that I can copy variables and their value from one object's namespace to another object's namespace with the same variable names automatically: b.__dict__.update(a.__dict__) The reason I'm posting this is to ask what to watch out for w

Re: __dict__ is neato torpedo!

2011-06-12 Thread Steven D'Aprano
On Sat, 11 Jun 2011 21:28:40 -0500, Andrew Berg wrote: > On 2011.06.11 09:13 PM, Steven D'Aprano wrote: >> A second, more subtle risk: not all objects have a __dict__. But if you >> obey the rule about never updating from arbitrary objects you don't >> know, then you won't be surprised by an obje

Re: __dict__ is neato torpedo!

2011-06-11 Thread Ian Kelly
On Sat, Jun 11, 2011 at 10:32 PM, Andrew Berg wrote: > On 2011.06.11 10:40 PM, Ben Finney wrote: >> It's exactly the same as with an ordinary assignment (‘a = b’) in >> Python. > Fair enough. >> > How would I make actual copies? >> At what level? > Level? I just want to be able to create an object

Re: __dict__ is neato torpedo!

2011-06-11 Thread Andrew Berg
On 2011.06.11 10:40 PM, Ben Finney wrote: > It's exactly the same as with an ordinary assignment (‘a = b’) in > Python. Fair enough. > > How would I make actual copies? > At what level? Level? I just want to be able to create an object b with values from dictionary a, and not have changes to a refl

Re: __dict__ is neato torpedo!

2011-06-11 Thread Ben Finney
Andrew Berg writes: > On 2011.06.11 10:08 PM, Ian Kelly wrote: > > For immutable objects such as ints, this doesn't matter. For mutable > > objects such as lists, it can: > Well, that's confusing. It's exactly the same as with an ordinary assignment (‘a = b’) in Python. You will likely want to w

Re: __dict__ is neato torpedo!

2011-06-11 Thread Andrew Berg
On 2011.06.11 10:08 PM, Ian Kelly wrote: > For immutable objects such as > ints, this doesn't matter. For mutable objects such as lists, it can: Well, that's confusing. How would I make actual copies? -- http://mail.python.org/mailman/listinfo/python-list

Re: __dict__ is neato torpedo!

2011-06-11 Thread Ian Kelly
On Sat, Jun 11, 2011 at 8:21 PM, Andrew Berg wrote: > On 2011.06.11 09:12 PM, Terry Reedy wrote: >> On 6/11/2011 9:32 PM, Andrew Berg wrote: >> > I'm pretty happy that I can copy variables and their value from one >> >> You are copying names and their associations, but not the objects or >> thier

Re: __dict__ is neato torpedo!

2011-06-11 Thread Andrew Berg
On 2011.06.11 09:13 PM, Steven D'Aprano wrote: > So never update from a random object you don't know well. Of course. In the project I'm working on, this will be used in the __init__() method of a class that accepts a pair of dictionaries or possibly **kwargs (for flexibility and to avoid the very

Re: __dict__ is neato torpedo!

2011-06-11 Thread Andrew Berg
On 2011.06.11 09:12 PM, Terry Reedy wrote: > On 6/11/2011 9:32 PM, Andrew Berg wrote: > > I'm pretty happy that I can copy variables and their value from one > > You are copying names and their associations, but not the objects or > thier values. Associations? The update() method copies the values

Re: __dict__ is neato torpedo!

2011-06-11 Thread Steven D'Aprano
On Sat, 11 Jun 2011 20:32:37 -0500, Andrew Berg wrote: > I'm pretty happy that I can copy variables and their value from one > object's namespace to another object's namespace with the same variable > names automatically: > > class simpleObject(): > pass > > a = simpleObject() > b = simpleOb

Re: __dict__ is neato torpedo!

2011-06-11 Thread Terry Reedy
On 6/11/2011 9:32 PM, Andrew Berg wrote: I'm pretty happy that I can copy variables and their value from one You are copying names and their associations, but not the objects or thier values. object's namespace to another object's namespace with the same variable names automatically: class

__dict__ is neato torpedo!

2011-06-11 Thread Andrew Berg
I'm pretty happy that I can copy variables and their value from one object's namespace to another object's namespace with the same variable names automatically: class simpleObject(): pass a = simpleObject() b = simpleObject() a.val1 = 1 a.val2 = 2 b.__dict__.update(a.__dict__) a.val1 = 'a'