static methods and meta classes 
Author Message
 static methods and meta classes

For reasons that are a bit complicated to explain I have developed a program
that passes classes around quite a bit as parameters.  I now need to
associate methods that can be invoked on the class objects themselves as
opposed to the instances of the class.  As an example, for a given class:

class Foo:
    def f(self, param): return param + 1

I need to invoke Foo.f(10).  This obviously won't work given that f is not a
static method.  So I thought I could to this:

class MetaFoo:
    def f(self, param): return param + 1

class Foo:
    __class__ = MetaFoo

In the hope that python could think of Foo as a class that is also an
instance of MetaFoo and allow me to invoke:

Foo.f(10)

This does not work.  I wonder why, every other undocumented crazy thing I
have tried in Python works.  Does anyone know how to trick Python into
supporting static methods?


appreciate it.

Thanks



Wed, 31 Dec 2003 05:26:43 GMT  
 static methods and meta classes

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52304


Quote:
> For reasons that are a bit complicated to explain I have developed a
program
> that passes classes around quite a bit as parameters.  I now need to
> associate methods that can be invoked on the class objects themselves as
> opposed to the instances of the class.  As an example, for a given class:

> class Foo:
>     def f(self, param): return param + 1

> I need to invoke Foo.f(10).  This obviously won't work given that f is not
a
> static method.  So I thought I could to this:

> class MetaFoo:
>     def f(self, param): return param + 1

> class Foo:
>     __class__ = MetaFoo

> In the hope that python could think of Foo as a class that is also an
> instance of MetaFoo and allow me to invoke:

> Foo.f(10)

> This does not work.  I wonder why, every other undocumented crazy thing I
> have tried in Python works.  Does anyone know how to trick Python into
> supporting static methods?


> appreciate it.

> Thanks



Wed, 31 Dec 2003 06:01:15 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Static method and class method comments

2. Static method and class method comments

3. BlackBox Meta: accessing methods through Meta

4. Static class methods

5. Class static methods

6. static class methods in Python?

7. static class methods/members?

8. Static (class) methods

9. implementing class (static) methods in MOP

10. How to create (static|class)-methods from C

11. determining method type (instance, static, class)

12. Python methods and C++ static methods

 

 
Powered by phpBB® Forum Software