I recently learned about the builtin function vars() and used it:
vars(...) vars([object]) -> dictionary Without arguments, equivalent to locals(). With an argument, equivalent to object.__dict__.
It looks to me more just a shortcut, creating a reference:
>>> class C(): pass ... >>> c = C() >>> c.__dict__ is vars(c) True
So, if you do something with one, you will see in another since they are the same:
>>> c.__dict__['foobar'] = True >>> vars(c)['foobar'] True
If you need to do something, say converting to dict and process from there for passing to a function, you will need to, at least:
>>> d = vars(c).copy() >>> d['new'] = True >>> 'new' in c.__dict__ False
It’s not deep copy, so you might want to be careful with that, or use copy.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.