473,398 Members | 2,088 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,398 software developers and data experts.

How to Adding Functionality to a Class by metaclass(not by inherit)

How to Adding Functionality to a Class by metaclass(not by inherit)

#example:

import inspect

class Foo(object):
def f(self):
pass

def g(self):
pass

class MetaFoo(type):
def __init__(cls, name, bases, dic):
super(MetaFoo, cls).__init__(name, bases, dic)

for n, f in inspect.getmembers(Foo, inspect.ismethod):
setattr(cls, n, f)

#Bar want to achieve Foo's part/all functionality, but not by inherit

class Bar(object):
__metaclass__ = MetaFoo

b = Bar()
b.f() TypeError: unbound method f() must be called with Foo instance as first argument (got nothing instead) Bar.f <unbound method Foo.f> b.f

<unbound method Foo.f>

how can I set Bar.f as <unbound method Bar.f>



Aug 12 '05 #1
1 1530
kyo guan wrote:
How to Adding Functionality to a Class by metaclass(not by inherit)
[snip]
class MetaFoo(type):
def __init__(cls, name, bases, dic):
super(MetaFoo, cls).__init__(name, bases, dic)

for n, f in inspect.getmembers(Foo, inspect.ismethod):
setattr(cls, n, f)

^^^^^^^^^
f.im_func

See if that works. I think it should.

But the real question is why you want to do this. Why can't you just
inherit from Foo?

STeVe
Aug 12 '05 #2

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

Similar topics

0
by: Kyo Guan | last post by:
How to Adding Functionality to a Class by metaclass(not by inherit) #example: import inspect class Foo(object): def f(self): pass
3
by: rua17 | last post by:
I add tow Com libraries to my project, I can see their content with intellisense, but when I instantiate a class contained in the library: PDDirectLib.PDDatabaseQuery pDbQuery = new...
0
by: Blueflower | last post by:
How can I casting (copying data + schema) from a DataTable (data source) to a object of class inherit from DataTAble Thanks,
0
by: papaja | last post by:
Hello, I believe I didn't change anything on my machine but my web applications, particularly pages accessing Microsoft Access DB, suddenly refused to work. I get this error: ...
0
by: Nonee | last post by:
Anyone, anyone? : ) Hello- I have a form with the mediaplayer referenced to play mp3's and avi's and I believe that is what is causing the problem. I am not sure, but I am hoping. ...
1
by: Paul W | last post by:
I attempted to find the solution and came close (http://blogs.msdn.com/robgruen/archive/2005/01/24/359660.aspx) but cannot go any further. I have an ASP.NET (2003/C#) app that calls a VB 6 DCOM on...
4
by: wingphil | last post by:
Hi all, I've got a class in my project called Location - it has some shared members that I can't reference from within a form because forms have a property called location. Short fo renaming the...
3
by: bradw2k | last post by:
In 2.0, can a partial class inherit from a partial class to override a virtual method? I do not have VS2005 installed to test, I'm just trying to plan with respect to upgrading to 2.0 later. If...
4
by: vasavimaruthi | last post by:
hi, i got class not found exception of a small jdbc program in windows. the program is like this import java.sql.*; public class Db1 { public static void main(String a)...
5
by: tshad | last post by:
I have the following class in my VS 2008 project that has a namespace of MyFunctions. ********************************* Imports System Imports System.Text.RegularExpressions Namespace...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.