Attack a sacred Python Cow
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sat Jul 26 08:24:28 EDT 2008
On Sat, 26 Jul 2008 11:08:12 +0200, Nikolaus Rath wrote:
> Terry Reedy <tjreedy at udel.edu> writes:
...
>> Because you must prefix self attributes with 'self.'. If you do not use
>> any attributes of the instance of the class you are making the function
>> an instance method of, then it is not really an instance method and
>> need not and I would say should not be masqueraded as one. If the
>> function is a static method, then it should be labeled as one and no
>> 'self' is not needed and auto insertion would be a mistake. In brief, I
>> assume the OP wants 'self' inserted in the body because inserting it
>> only in the parameter list and never using it in the body is either
>> silly or wrong.
>
>
> I think you misunderstood him. What he wants is to write
>
>
> class foo:
> def bar(arg):
> self.whatever = arg + 1
>
>
> instead of
>
> class foo:
> def bar(self, arg)
> self.whatever = arg + 1
>
>
> so 'self' should *automatically* only be inserted in the function
> declaration, and *manually* be typed for attributes.
That idea might have worked many years ago, but not now. The problem is,
what happens here?
class Foo(object):
def foo(self, arg):
self.whatever = arg + 1
@classmethod
def cfoo(cls, arg):
cls.whatever = arg - 1
@staticmethod
def sfoo(arg):
print arg
How does the compiler know to insert "self" into the argument list for
foo, "cls" into that of cfoo, but do nothing for sfoo? Decorators can
transform methods in arbitrarily complex ways using the Descriptor
protocol.
--
Steven
More information about the Python-list
mailing list