Python methods and C++ static methods

Yann Cointepas yann.cointepas at chups.jussieu.fr
Fri Apr 27 10:48:17 EDT 2001


Can Python unbound methods easily be used like C++ static methods ?

The problem is that unbound methods must be called with instance as first 
argument. Therefore the following code fails.

>>> class C:
...   def static_method( i ):
...     return 2 * i
...
>>> x = C.static_method( 3 )
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unbound method static_method() must be called with instance as 
first argument

I would like to keep 'static_method' in 'C' attributes but I think that it 
is not possible. Am I wrong ? If I am not, I plan to use the following code:

class C:
  class foo:
    def static_method( self, i ):
       return 2 * i
  static = foo()

x = C.static.static_method( 3 )

Is there a better solution ?

Thanks for any help.
-- 
Yann Cointepas                        Tel: +33 1 53 82 84 14
INSERM U494 - CHU Pitié-Salpêtrière   Fax: +33 1 53 82 84 48
91 Boulevard de l'Hôpital
75634 PARIS CEDEX 13 France



More information about the Python-list mailing list