Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Downcast (I know it sounds silly, but ...)

945 views
Skip to first unread message

Rocco Rossi

unread,
Jan 2, 2003, 3:04:24 PM1/2/03
to
I was wondering if it is possible in Python to do something analogous to
downcasting in a statically typed language like C++. I mean is it possible
to take an instance of a base class and magically transform it into an
instance of some derived class eventually fixing things (like the missing
attribute data)?

Thanx.

John Ochiltree

unread,
Jan 2, 2003, 3:20:36 PM1/2/03
to
Rocco Rossi wrote:

Yes

Terry Reedy

unread,
Jan 2, 2003, 3:53:29 PM1/2/03
to

"Rocco Rossi" <rockr...@libero.it> wrote in message
news:cX0R9.72667$Ou4.2...@twister2.libero.it...

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'


Jp Calderone

unread,
Jan 2, 2003, 3:29:09 PM1/2/03
to

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

Martin v. Löwis

unread,
Jan 2, 2003, 4:21:47 PM1/2/03
to
John Ochiltree <johnoc...@blueyonder.co.uk> writes:

> Yes

:-)

Peter Hansen

unread,
Jan 2, 2003, 7:17:53 PM1/2/03
to

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

Stuart D. Gathman

unread,
Jan 2, 2003, 7:35:37 PM1/2/03
to

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.

Erik Max Francis

unread,
Jan 3, 2003, 2:21:08 AM1/3/03
to
Rocco Rossi wrote:

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.

0 new messages