dynamism

Greg Ewing see_reply_address at something.invalid
Tue Sep 10 01:32:47 EDT 2002


Steven Shaw wrote:

> 
> I thought you could just extend an object's attributes at run-time like this:
> 
>>>>p = object()
>>>>p.x = 1


It seems that you can't do this to an instance
of "object" itself, because it's a built-in type.
But you can do it to a subclass of object:

 >>> class C(object):
...  pass
...
 >>> c = C()
 >>> c.x = 1
 >>> print c.x
1
 >>>


> I thought that you could access dictionary values with object syntax:
> 
>>>>x = { 'a': 1, 'b':2 }
>>>>x.a


No, that has never been possible. But instances have
a __dict__ attribute that holds all the other
attributes, so you can do this:

 >>> x.__dict__['a'] = 42
 >>> print x.a
42
 >>>

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list