Forgetting "()" when calling methods

Alex Martelli aleax at aleax.it
Sun Apr 27 03:45:07 EDT 2003


Terry Reedy wrote:

> "Erik Max Francis" <max at alcyone.com> wrote in message
> news:3EAB6A14.707AD826 at alcyone.com...
>> Jeremy Fincher wrote:
>>
>> > Perhaps the answer is to remove the __nonzero__ method on
>> > functions/methods.  Logically, what purpose does it serve?
>>
>> # f holds a callable object or None
>> if f:
>>     f(...)
> 
> or
> print f and f() or 'cannot do that' # assumes boo(f()) always True
> 
> I consider it a feature that all objects have truth value (except for
> perversely written user classes), so that conditional expressions and
> statements always work without exception.

It's a feature, just like it WAS a feature that you USED TO be able
to always test "if a>b:" without exception.  But features can and do
get removed when it's deemed too likely that they "let errors pass
silently" -- the ability to compare a>b unrestrictedly was removed
in the move 2.0 -> 2.1 (when either comparand is a complex and the
other any kind of number) and it may get restricted even further.  By
the same token, the ability to "use the truth value" of a function or
method object COULD be removed for similar reasons.

It's sad that this enhancement cannot be used, to support such tests
as "if f:" rather than "if f is not None:".  But -- I would HOPE
somebody tried to make to the BDFL that "the use case" for keeping
functions and methods 'true' would be that horrid single-line print,
so unreadable, fragile, and unPythonic... if you managed to convince
him that THIS is the usage that such truth values empower, then I
think we'd see the truth value go away by 2.3 beta 2!-)


Alex





More information about the Python-list mailing list