LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-10-2010, 05:28 PM   #1
CoderMan
Member
 
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
Dan04
Member
 
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
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375

Original Poster
Blog Entries: 24

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.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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