Question about metaclass

M

Makoto Kuwata

Hi,

I want to define a special class which groups functions, like:

class Greepting(FuncGroup):
def hello(): # no self, no @staticmethod!
print("Hello!")
def goodbye(): # no self, no @staticmethod!
print("Good Bye!")

Geeting.hello(): #=> "Hello!"
Geeting.goodbye(): #=> "Good Bye!"


I tried the following code which converts instance mthods into
static method automatically, but I don't get result what I want.
(python 2.5.5)

import sys
from types import FunctionType

class MetaClass(type):
def __init__(cls, name, bases, dct):
## converts instance methods to static methods automatically
for k in dct.keys():
v = dct[k]
if isinstance(v, FunctionType):
dct[k] = staticmethod(v)
print("*** debug: dct[%r] = %r" % (k, dct[k]))
#=> <staticmethod object at 0x100378d38>

class FuncGroup(object):
__metaclass__ = MetaClass

class Greeting(FuncGroup):
def hello():
print("Hello!")
def goodbye():
print("Good Bye!")

print("*** type(Greeting.hello)=%r" % type(Greeting.hello)
#=> <type 'instancemthod'>
print("*** type(Greeting.goodbye)=%r" % type(Greeting.goodbye)
#=> <type 'instancemthod'>


Could you give me advice?
 
P

Patrick Maupin

Hi,

I want to define a special class which groups functions, like:

    class Greepting(FuncGroup):
        def hello():          # no self, no @staticmethod!
            print("Hello!")
        def goodbye():        # no self, no @staticmethod!
            print("Good Bye!")

    Geeting.hello():    #=> "Hello!"
    Geeting.goodbye():  #=> "Good Bye!"

I tried the following code which converts instance mthods into
static method automatically, but I don't get result what I want.
(python 2.5.5)

    import sys
    from types import FunctionType

    class MetaClass(type):
        def __init__(cls, name, bases, dct):
            ## converts instance methods to static methods automatically
            for k in dct.keys():
                v = dct[k]
                if isinstance(v, FunctionType):
                    dct[k] = staticmethod(v)
                    print("*** debug: dct[%r] = %r"% (k, dct[k]))
#=> <staticmethod object at 0x100378d38>

    class FuncGroup(object):
        __metaclass__ = MetaClass

    class Greeting(FuncGroup):
        def hello():
            print("Hello!")
        def goodbye():
            print("Good Bye!")

    print("*** type(Greeting.hello)=%r" % type(Greeting.hello)
#=> <type 'instancemthod'>
    print("*** type(Greeting.goodbye)=%r" % type(Greeting.goodbye)
#=> <type 'instancemthod'>

Could you give me advice?

I think you need to unwrap the instance methods first:
.... def bar():
.... print "Hi there"
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top