Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-10-2010, 05:28 PM   #1
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
Python n00b: promoting a derived class to base class?

I have an situation kind of like so: I have object b of class B, which is a derived from base class A. Is there a way I can cast or reassign object b so that it (or the new variable referring to it) it only has the interface of class A?

Obviously, polymorphism allows me to use object b anywhere as though it was simply a class A object. However, it would be a little more convenient if I could recast it as a class A object, so that I would never have to worry about unknowingly using a method from class B's interface.

In Java I would just assign object b to a Class A variable.
Old 03-11-2010, 12:52 AM   #2
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
It goes against the spirit of the language. Python programs rely on duck typing.

If you really want to do it, you can write an Upcast class that defines __getattribute__ appropriately.
Old 03-11-2010, 01:46 PM   #3
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Original Poster
Rep: Reputation: 43
Hmm. I read the link, and that makes sense. Python isn't statically typed anyway, and functions really don't care what kind of object they receive, there isn't much point in trying to recast objects in the way I described.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP:: Can I have virtual functions in derived class?? < Are they restricted to Base> caruna Programming 3 12-22-2009 10:17 AM
C++: derived class? unihiekka Programming 4 02-03-2009 09:40 AM
Derived class inside parent class array, possible? xemous Programming 3 10-17-2006 11:35 AM
Does derivated class inherit base class destructor (constructor)? kornerr Programming 2 08-23-2006 08:05 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:04 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration