473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Mixing classes...

Is this possible:

class C1:
def somemethods(self):
"""methods do stuff, create member variables"""
pass

def f1():
"""function returns instances of C1, somewhat manipulated into specific
states"""
return C1()
class C2(C1): # C2 derives from C1
def othermethods(self):
"""new methods are introduced, but no new member variables (they work
on existing ones)"""
pass

# So far, everything's ok. Now, i want to create instance of
# C1 using f1:

c = f1() # could be equal to "c=C1()", but not always...

# ... and somehow "add" or "overlay" the additional methods
# declared in C2 on the object "c". I tried this:

c.othermethods=C2.othermethods

c.othermethods() # fails here, is confused about what is 'self'
#######
While this is probably considered "strange", and it's certainly not good
design style, I somehow feel it could be possible to do in python. Any
clues?

Jul 18 '05 #1
1 1405
Ivan Voras wrote:
Is this possible:

class C1:
def somemethods(self):
"""methods do stuff, create member variables"""
pass

def f1():
"""function returns instances of C1, somewhat manipulated into
specific states"""
return C1()
class C2(C1): # C2 derives from C1
def othermethods(self):
"""new methods are introduced, but no new member variables (they
work on existing ones)"""
pass

# So far, everything's ok. Now, i want to create instance of
# C1 using f1:

c = f1() # could be equal to "c=C1()", but not always...

# ... and somehow "add" or "overlay" the additional methods
# declared in C2 on the object "c". I tried this:

c.othermethods=C2.othermethods

c.othermethods() # fails here, is confused about what is 'self'
#######
While this is probably considered "strange", and it's certainly not good
design style, I somehow feel it could be possible to do in python. Any
clues?


Use new.instancemethod:
class C1: .... def somemethods(self):
.... print 'somemethods'
.... def f1(): .... return C1()
.... class C2(C1): .... def othermethods(self):
.... print 'othermethods'
.... c = f1()
c.somemethods() somemethods import new
c.othermethods = new.instancemethod(C2.othermethods.im_func, c, c.__class__)
c.othermethods()

othermethods

HTH,

--
Hans Nowak (ha**@zephyrfalcon.org)
http://zephyrfalcon.org/

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kamal | last post by:
Is it possible to implement video mixing technique (video merging technique) in JMF, where sources of the video data are remote hosts across the network transmitting real-time data??? The output...
6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
0
by: Erik Max Francis | last post by:
Is there any prohibition against mixing different protocols within the same pickle? I don't see anything about this in the Python Library Reference and, after all, the pickle.dump function takes a...
4
by: Rudolf | last post by:
Is it possible to add a vb.net source code module to a c# project and if so how? Thanks Rudolf
4
by: Cristian Tota | last post by:
Hi, I'd appreciate any thoughts on mixing C++ and C code. I have a project that uses a given C interface, the rest of the project can be either in C or C++. What would be the recomended design...
3
by: Steven Quail | last post by:
Hi to all, Having come from a delphi environment, I remember that when using interfaces, we were told not to mix the object models and interface models. In other words, when I create an object,...
4
by: RML | last post by:
Hi all, Can an MFC C++ (VS 2005) project have .c files that contain C style functions? Can these C functions use the classes created in the project? RML
3
by: jason | last post by:
I've been working with C# for over a year now without touching vb.net code. I had a few light years of vb.net before that. No real vb6 or windows form experience. Suddenly, I have an assignment...
5
by: Budde, Marco | last post by:
We have developed a big library using managed C++. The project started with .NET 1.0 and has been ported to .NET 1.1. Today we ship the library compiled for .NET 1.1 and 2.0. At the moment the...
2
by: Jon Slaughter | last post by:
How difficult is it for one to integrate unmanaged C++ into C#? I know for functions one can use DLLimport but how does one go about doing it for classes? Do I have to completely reimplement the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.