dynamism

Mark McEahern marklists at mceahern.com
Mon Sep 9 23:36:37 EDT 2002


[Steven Shaw]
> I've been away from Python for awhile and I'm wondering whether Python got
> less "dynamic" while I was away.
>
> I thought you could just extend an object's attributes at
> run-time like this:
>
> >>> p = object()
> >>> p.x = 1
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> AttributeError: 'object' object has no attribute 'x'

object is the base type of new-style classes.  Try something like this
instead:

>>> class foo:pass
...
>>> f = foo()
>>> f.x = 1
>>> f.x
1

Or 'class foo(object):pass' works too.

// m

-





More information about the Python-list mailing list