Method or function?

Rainer Deyke root at rainerdeyke.com
Thu Nov 2 02:23:41 EST 2000


"Quinn Dunkan" <quinn at dinar.ugcs.caltech.edu> wrote in message
news:slrn902275.43c.quinn at dinar.ugcs.caltech.edu...
> On Tue, 31 Oct 2000 19:17:37 GMT, Rainer Deyke <root at rainerdeyke.com>
wrote:
> >"Dale Strickland-Clark" <dale at out-think.NOSPAMco.uk> wrote in message
> >news:c3dtvs0uuleerbk6i44psnvmhqug6ghoec at 4ax.com...
> >> float
> >
> >This is a constructor function.
>
> But constructors *are* methods.

Constructor function = function that creates a new object.  This is
different from the concept of a constructor in the typical OO sense, which
initializes an object which already exists (in the physical sense, i.e. its
memory is already allocated).  Maybe I should use the term "factory
function" instead.  In Python, classes themselves behave like factory
function (i.e. they can be called to produce a new object).  Should all
classes be methods of other classes?  (Hint: no.)

> >> id
> >
> >id applies to all objects, including methods.  Do you really want
> >1.id().id().id() to be legal Python?
>
> Why not?  We already have 'h'[0][0][0][0] :)  In ruby: '1.id.id.id.id.id'
> And in python we already have id(id(id(id)))... would you prefer that be
> illegal?  Why?

I meant 1.id.id.id(), or simply 1.id.id().  The id method of a bound method.
I suppose there is a certain elegance to bound methods themselves having
methods...

> >> map
> >
> >As method of what?  The function?  The sequence?  Which one?
>
> The sequence, silly.  Smalltalk calls it 'collect'.

map accepts several sequences as arguments, hence my question "Which one?"

> >> max
> >> min
> >
> >1.max(2) # Evaluates to 2
> >1.min(2) # Evalutes to 1
> >
> >I rest my case.
>
> What?  It looks fine to me.  Does it bother you that '1>2' evaluetes to 0,
> while '1>0' evaluetes to 1?

It bothers me that a function with two (or more) arguments which are treated
equally is implemented as a method of one of the arguments.  It's asymetric.

If I want to create a new numeric class which can be used with existing
numbers in addition, I have to define two methods for the implementing
class: __add__ and __radd__ (ignoring the possibility of __coerce__ for
now).  This is messy.  WIth min and max there is a similar situation, except
that there is no conceivable reason for overriding the behavior of min/max
for multiple arguments.

In general, methods should be used for only two reasons: encapsulation and
polymorphism.  In all other cases, functions are more appropriate.  OO
purism is a disease.


>  Also consider:
>
> def clamp(lst, maxval):
>     return lst.filter(maxval.max)

Use lambda.

> >> ord
> >
> >This would make sense as a method, somewhat.
>
> But I thought
> >                                                         Methods in
general
> >should be applicable to all instances of a type/class.
> what's the value of 'ab'.ord()?

It makes sense (to me) to tell a (single-character) string to convert itself
into a number.  It does not make sense to tell a string to evaluate itself
as a Python expression, since it is not reasonable to expect a string how to
do that.  Multi-character string can throw an exception on attempts to
access their 'ord' attribute. :-)

> And wouldn't that make the string type "dependent" on the integer type
(which
> is a bad idea)?

Integers are more primitive than strings.  Note that strings (like all
sequences) are indexed by integers, while integers exist independently from
strings.

> :)

:-P

> >> round
> >
> >This might make sense as a method.
>
> But it's a constructor function!  It makes floats!  How is
>
> number.float()
>
> different from
>
> number.round(9999999)

'round' creates a float from a float.  'float' creates a float from a
non-float (excepting float(1.0) silliness).  They are both floating point
operations, but only the former could be implemented as a mthod of floating
point.  The latter, if implemented as a method, would have to be placed in
non-float types/classes, where it wouldn't belong.

> >> str
> >
> >See: repr
>
> sather:
> #OUT + obj.str + "\n";

Your point being?

> >> vars
> >
> >See: dir
>
> ruby:
> 1.methods #-> ["abs", "**", "next", "<=>", "-", "to_f", ...]

Python:
>>>[].__methods__
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse',
'sort']


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list