Why there is a parameter named "self" for classmethod function?

Aaron Brady castironpi at gmail.com
Sat May 9 23:33:06 CEST 2009


On May 8, 7:52 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Thu, 07 May 2009 04:27:15 -0700, Aaron Brady wrote:
> >> Can be, but if there's reason enough to keep it with a class, there's
> >> no reason not to.
>
> > That's a bit of hyperbole; the usual reasons such as code bloat,
> > namespace bloat, maintainability etc. all weigh against it.  It's just
> > that the benefits weigh heavier.
>
> I'm not sure I understand. Whether the function is in the class or
> extracted out into the module, it is exactly the same function. (Apart
> from an extra level of indentation, and either an unused "self" parameter
> or a @staticmethod line.) Same amount of code, same difficulty of
> maintainability. Namespace bloat is also the same, the only difference
> being which namespace is bloated: the class or the method.
>
> --
> Steven

My post was a little harsh.  I meant to say that I think I got your
idea, but the form it came in was a tautology.  Its form was X if not
not X.  Tautologies come across as particularly low bandwidth in
writing, though they do have some content in context.  I observe
people say them a lot too: 'it is what it is', 'if I want to I want
to', 'two bucks is two bucks', etc.  Some of the context comes from
the partial conventional asymmetry of sentences in English between
subject and predicate (nominative and accusative), though there is
none in logic.  Not to mention, 'hyperbole' isn't the word for what I
was thinking either... ahem.

The best place for such a function between those two choices can
depend on many things, some of which are matters of taste.  If the
definition occurs in the class, it may be more convenient to read or
edit.  That difference is pretty negligible.  There is semantic
difference to inherited classes: whether you have to import the class
or the class /and/ the function.  Then when it is called, the form
will be either 'class.methodA' or just 'methodA', which makes a
difference in readability (obviousness) elsewhere that it is called.

> being which namespace is bloated: the class or the method.

I think you meant 'class or module'.



More information about the Python-list mailing list