Using metaclasses to play with decorators.

Duncan Booth me at privacy.net
Wed Jun 23 14:26:08 CEST 2004


"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
>    def methodA(x, y):
>      return x + y
> 
> What has been gained by the added syntactic clutter?

Does this help?

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