noob question: "TypeError" wrong number of args

bruno at modulix onurb at xiludom.gro
Wed May 3 13:56:11 EDT 2006


Edward Elliott wrote:
> Bruno Desthuilliers wrote:
> 
>>But then, constructs like:
>>
>>class Obj(object):
>>   def method(...): ...
>>   method = staticmethod(method)
>>
>>or it's newer syntactic-sugar-version would become somewhat more
>>difficult to parse properly - but I admit that this is beyond my
>>knowledge.
> 
> 
> Hmm, that'll take some thought.  I assume if you're making method static,
> you don't declare a self parameter for it? 

Nope. But classmethods takes the class object as the first param
(usually named 'cls').

> 
>>>e.g. when it sees "def method (a,b)"
>>
>>This has to be in a class statement - else it's shouldn't be touched.
> 
> Exactly, that's why I called it method and not func.

Technically, they are still function objects. They are later wrapped
into method descriptor objects (at lookup time IIRC, but ask a guru or
read the doc to know for sure). And don't forget the case of nested
functions...

>  If you're declaring
> methods outside the class and adding them dynamically, I think you would
> need to declare 'self' explicitly.

Of course. Virtually *any* function can be set as a method[1] outside
the class statement scope, either directly on the class object or - with
manual wrapping - on an individual instance. And it's important to keep
this feature.

(snip)

>>>I can prove that assertion too: make a simple text processor that reads
>>>Python source code and outputs the same source code with only one change:
>>>insert the string 'self" as the first parameter of every "def
>>>somemethod".
>>
>>Not so simple: you have plain functions (outside class statements), and
>>inner functions (inside def statements), and classmethods, and statict
>>methods, and some other corner cases (like __new__()) to take into
>>acccount... 
> 
> 
> Well I'm assuming the rules for when to put 'self' in the parameter list are
> simple enough to be automated. 

They are still complex enough to require a real parser. And anyway,
since this is a syntax change, this should be handled by the parser IMHO.

(snip)
> 
>>You'll have to really prove it doesn't break anything else in the 
>>object model, have strong and articulate arguments to support your point
>>(like proving this is really a common source of confusion for newbies),
>>and I really doubt it'll be accepted anyway. But what...
> 
> 
> It's definitely an uphill battle and I wonder if it's really worth the
> effort. 

Honestly, I don't think it has much chance. I barely even notice typing
'self' or 'cls',   and I guess it's the same for most Python programmers
- those who can't stand it probably use another language... And FWIW, I
still prefer to keep consistency in functions definitions.


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list