Difference between static method and a function as a class at tribute?

sismex01 at hebmex.com sismex01 at hebmex.com
Fri Sep 6 10:08:57 EDT 2002


> 
> Hi,
> 
> What are the differences, if any, between 1 and 2?
> 
> 1.
> def funtest(*args):
>     pass
> 
> class test(object):
>     method = funtest
> 
> 2.
> class test(object):
>     def funtest(*args):
>         pass
> 
>     method = staticmethod(funtest)
> 
> 
> And if there are any differences do they really matter, that is, does
> anybody has any test cases where 1 and 2 are not interchangeable?
> 
> TIA and all the best,
> Gonçalo Rodrigues

Take a look at this:

>>> class testclass:
... 	pass
... 
>>> 
>>> def functest(*args):
... 	print args
... 	
>>> 
>>> testclass.testmethod = functest
>>> 
>>> inst = testclass()
>>> dir(inst)
['__doc__', '__module__', 'testmethod']
>>> 
>>> inst.testmethod()
(<__main__.testclass instance at 0x013A5020>,)
>>> 

Any "plain" executable found as an attribute of a class
object, upon creating an instance of that class, is bound
to the instance as a method.  If not, then calling
"inst.testmethod()" should display an empty tuple.

I suspect that staticmethod(func) and classmethod(func)
wrap the function in a proxy which first extracts some
info from the actuall call (say, the instance's class)
and pass *whose* arguments to the real function.

Neat. A tribute to Python's true power, that you
can do that and not pollute the language with
wierd "special cases" and such.

-gus
--

Advertencia: 
La informacion contenida en este mensaje es confidencial y restringida y
esta destinada unicamente para el uso de la persona arriba indicada, Esta
comunicacion representa la opinion personal del remitente y no refleja
necesariamente la opinion de la Compañia. Se le notifica que esta
estrictamente prohibida cualquier difusion, distribucion o copia de este
mensaje. Si ha recibido esta comunicacion o copia de este mensaje por error,
o si hay problemas en la transmision, favor de comunicarse con el remitente.


Todo el correo electrónico enviado para o desde esta dirección será
procesado por el sistema de correo corporativo de HEB. Tal correo
electrónico esta sujeto a ser almacenado y puede ser revisado por alguien
ajeno al recipiente autorizado con el propósito de monitorear que se cumplan
las normas de seguridad de la empresa.




More information about the Python-list mailing list