Thanx.
Yes
Maybe. If you can modify __class__ to the derived class, yes. If you
cannot, no.
You can only do so with new-style classes if the layouts are
'compatible' -- but I do not know the rules for what is.
With 2.2.1:
>>> class C: pass
...
>>> class CC(C): pass
...
>>> c = C()
>>> c.__class__
<class __main__.C at 0x00794ED0>
>>> c.__class__ = CC
>>> c.__class__
<class __main__.CC at 0x007940A0>
>>> class N(object): pass
...
>>> n = object()
>>> n.__class__
<type 'object'>
>>> n.__class__ = N
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __class__ assignment: 'N' object layout differs from
'object'
>>> class NN(N): pass
...
>>> n=N()
>>> n.__class__
<class '__main__.N'>
>>> N.__class__ = NN
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __class__ assignment: 'NN' object layout differs from
'type'
Since you can't cast in Python, the short answer is no. A different
answer might take the form of...
class Foo:
def foo(self):
print 'I am foo'
class Bar:
def bar(self):
print 'I am bar'
f = Foo()
f.foo()
f.__class__ = Bar
f.bar()
Notice that Bar isn't even a subclass of Foo. I'll refrain from pointing
out that downcasting an "object" in C++ to a class lower in the inheritance
hierarchy than it actually is is an error and only works because C++ is
weakly typed (because this isn't comp.lang.c++ ;)
hoping-you-don't-do-this'ly yours,
Jp
--
In the days when Sussman was a novice Minsky once came to him as he sat
hacking at the PDP-6. "What are you doing?" asked Minsky. "I am training a
randomly wired neural net to play Tic-Tac-Toe." "Why is the net wired
randomly?" asked Minsky. "I do not want it to have any preconceptions of how
to play." Minsky shut his eyes. "Why do you close your eyes?" Sussman asked
his teacher. "So the room will be empty." At that moment, Sussman was
enlightened.
--
12:00am up 17 days, 9:46, 3 users, load average: 0.70, 0.74, 0.79
You might want to explain why you think you need that (if this
was not just idle curiosity).
Then others here will explain why you are wrong, and how to go
about it in the Right Way.
:-)
-Peter
Downcasting in C++ is something you do to pointers, not to instances.
Here is how you "downcast" in Python:
class Base:
def foo(self): print "base"
class Derived(Base):
def bar(self): print "derived"
baseptr = Derived() # C++: Base *baseptr = new Derived();
baseptr.bar() # C++: ((Derived)baseptr)->bar();
# prints "derived"
Note that you do it exactly the same as in C++ - but without any type
attached to the reference variables (only instances have types in Python).
--
Stuart D. Gathman <stu...@bmsi.com>
Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.
Since Python has neither casts nor pointers (downcasting is done via
pointers), it's hard to tell precisely what you mean with respect to
Python. Perhaps if you back up a step and tell us what you are trying
to do?
Certainly with Python you can do all sorts of naughty tricks, such as
manually changing the instance of a class via setting its __class__
attribute. Whether or not that's really what you want depends on what
you're really after.
--
Erik Max Francis / m...@alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ We grow in time to trust the future for our answers.
\__/ Ruth Benedict
Physics reference / http://www.alcyone.com/max/reference/physics/
A physics reference.