noob question: "TypeError" wrong number of args

Edward Elliott nobody at 127.0.0.1
Tue May 2 20:49:39 EDT 2006


Ben Finney wrote:
> So now you're proposing that this be a special case when a function is
> declared by that particular syntax, and it should be different to when
> a function is created outside the class definition and added as a
> method to the object at run-time.
> 
> Thus breaking not only "explicit is better than implicit", but also
> "special cases aren't special enough to break the rules".

Exactly.

Hey, 'for' and 'while' are only special cases of if/goto.  Why not ditch
them and get back to basics?

Rules are made to be broken, the key is when.

Method calls are special cases no matter how you slice it.  Overall I think
implicit self beats explicit self for the typical case:

def method (a):
    self.a = a   # self magically appears
obj.method (x)

vs

def method (self, a):  # self explicit
    self.a = a
obj.method (x)  # arg count mismatch (except in message passing model)

Not so much for the argument mismatch problem (see start of thread), which
is easily rectified.  The former is simply more elegant in my view.  Less
clutter, less confusion.

Sure, if we get into calling "class.method (obj, a)" the argument mismatch
problem resurfaces with implicit self.  But 1) this is a rarer case, and 2)
that's not my primary objection anyway.

As long as we're trotting out aphorisms, how about DRY: Don't Repeat
Yourself.  The rule couldn't be clearer: don't repeat your SELF. ;) Yet
that's exactly what explicitly declaring self does, forces me to needlessly
repeat what everyone already knows: methods take the object instance as
their first parameter.

Whether this is a good idea is subject to debate, and I'd like to hear
discussion on the merits.  What I don't want is a silly battle of maxims.




More information about the Python-list mailing list