[Python-Dev] Re: PEP 318: Can't we all just get along?

James Y Knight foom at fuhm.net
Thu Aug 19 20:59:03 CEST 2004


On Aug 19, 2004, at 7:45 AM, Paul Morrow wrote:
> The vast majority of instance methods I've seen all use 'self' as the 
> first parameter.  Likewise, most class methods use 'cls' or 'klass' as 
> their first parameter.  If we exploit these conventions, we end up 
> with a simple, clear, obvious mechanism for denoting (this aspect of) 
> a method's type.
>
>     class Foo(Object):
>         def m1(self, a, b):    # this is an instance method of Foo
>             pass
>
>         def m2(cls, a, b):     # this is a class method of Foo
>             pass
>
>         def m3(a, b):          # this is a static method of Foo
>             pass
>
> A special Object (capital 'O') class could work this magic so that old 
> code didn't break.
>
> I know that this is odd.  But then so are most of the great things 
> about Python.

You can do that today. See also 
http://www.python.org/pycon/dc2004/papers/48/conveniencytypes.py

However, note that IMO it is quite rude to use a metaclass (or your 
capital O object -- same thing) to do this, as it will break any 
objects inheriting from your class that don't expect the strange 
automatic behavior. This auto-class/staticmethod-ification should be 
local to your code, and thus is really a candidate for a class 
decorator.

@automethods
class Foo(object):
   ...

James



More information about the Python-Dev mailing list