object vs class oriented -- xotcl
Jonathan Gardner
jgardner at jonathangardner.net
Thu Jan 24 16:13:49 EST 2008
On Jan 24, 12:35 pm, William Pursell <bill.purs... at gmail.com> wrote:
> I'm not sure that describes the method well. Basically, you can
> instantiate an object A of class Foo, and later change A to be an
> object of class Bar. Does Python support this type of flexibility?
> As I stated above, I've been away from Python for awhile now, and am a
> bit rusty, but it seems that slots or "new style" objects might
> provide this type of behavior. The ability to have an object change
> class is certainly (to me) a novel idea. Can I do it in Python?
Short answer: yes, easily.
Long answer: observe.
>>> class Foo(object):
... def foo(self): print "Foo.foo"
...
>>> class Bar(object):
... def foo(self): print "Bar.foo"
...
>>> a = Foo()
>>> a.__class__ = Bar
>>> a.foo()
Bar.foo
More information about the Python-list
mailing list