Using metaclasses to play with decorators.

Colin J. Williams cjw at sympatico.ca
Thu Jun 24 13:57:12 CEST 2004


Duncan Booth wrote:
> "Colin J. Williams" <cjw at sympatico.ca> wrote in 
> news:uRdCc.27945$Nz.1231193 at news20.bellglobal.com:
> 
> 
>>OK, I'll ignore 'staticmethod', but could you tell me how
>>
>>   def methodA(x, y) [noself]:
>>     return x + y
>>
>>differs in substance from
>>
>>   def methodA(self, y):
>>     return self + y
>>
>>or
   class Z:
      def __init__(x, a):
        x.a= a
>>   def methodA(x, y):
>>     return x + y
>>
>>What has been gained by the added syntactic clutter?
> 
> 
> Does this help?
Yes, this clarifies the static method idea.

Thanks,

Colin W.
> 
> class X:
>    def methodA(x, y) [ staticmethod ]:
>       return x+y
> 
>    def methodB(self,y):
>       return self+y
> 
>    def methodC(x,y):
>       return x+y
> 
> anX = X()
> 
> anX.methodA(2, 3) # returns 5
> anX.methodB(2, 3) # Exception: too many arguments
> anX.methodC(2, 3) # Exception: too many arguments
> 
> X.methodA(2, 3) # returns 5
> X.methodB(2, 3) # Exception: First argument must be an instance of X
> X.methodC(2, 3) # Exception: First argument must be an instance of X




More information about the Python-list mailing list