Method or function?

Rob W. W. Hooft rob at hooft.net
Sun Nov 5 15:12:01 EST 2000


>>>>> "TW" == Thomas Wouters <thomas at xs4all.net> writes:

 TW> On Fri, Nov 03, 2000 at 10:27:31AM +0100, Rob Hooft wrote:
 >> >>>>> "CC" == Cliff Crawford <cjc26 at nospam.cornell.edu> writes:

 CC> some of the functions on your list, like abs, only apply to
 CC> numbers; do you really want to write (-4).abs() instead? ;)

 >> Not to mention the problems with -4.abs() and -4.0.abs() [the
 >> method should bind tighter than the unary minus?] [Does this
 >> parse?]

 TW> [...]
 TW> As for what binds tighter, the minus or the method call, that's
 TW> easy. Not because it's obvious what the answer should be, but
 TW> because it's already perfectly valid syntax. 

Sure, people are using this -obj.attr syntax all the time, but that
doesn't mean that it's clear to write "-obj.abs()". For most methods
of numeric types the order does not matter, but for "abs" and "floor"
functions making them a method would allow code that needs more than
one look before one can decide what it is doing (like
"-4**2"). cf. also the reason why the following code is forbidden:

try:
    x
except:
    y
finally:
    z

[What happens if "y" returns or raises an exception?]

Rob
-- 
=====   rob at hooft.net          http://www.hooft.net/people/rob/  =====
=====   R&D, Nonius BV, Delft  http://www.nonius.nl/             =====
===== PGPid 0xFA19277D ========================== Use Linux! =========




More information about the Python-list mailing list