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

Paul Morrow pm_mon at yahoo.com
Thu Aug 19 21:30:38 CEST 2004


James Y Knight wrote:

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

Aha! Thanks!

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): ...

But then wouldn't subclasses of Foo have the same rude behavior? In fact, isn't the use of any metaclass rude, by your definition, as it will change the default class behavior in some way?



More information about the Python-Dev mailing list