Method or function?
Rainer Deyke
root at rainerdeyke.com
Tue Oct 31 14:17:37 EST 2000
"Dale Strickland-Clark" <dale at out-think.NOSPAMco.uk> wrote in message
news:c3dtvs0uuleerbk6i44psnvmhqug6ghoec at 4ax.com...
> I dare say that this has been discussed before, I just can't see it
> anywhere.
>
> Shouldn't these standard functions be methods of their various
> datatype classes?
The number of methods of an object should be minimal, so that it is easier
to implement new classes/types with the same external interface.
> These are just the ones that seem obvious to me. There are a few more
> than I'm not sure about.
>
> abs
This one makes sense as a method, because there is no generic algorithm to
get the absolute value. For example, calculating the absolute value of a
complex number requires a square root.
> chr
As a method of integer? That would make the integer type dependent on the
string type, which is generally a bad idea.
> dir
The whole point of dir is that it can be applied to all objects (including
class instances) and it automatically works.
> eval
As a method of string? Is there any advantage to this? Methods in general
should be applicable to all instances of a type/class.
> execfile
See my comment of eval.
> filter
Filter applies to all sequences. Do you want to write a new filter function
for each new sequence?
> float
This is a constructor function.
> hex
Suppose I want to write a function that converts an integer to a string in
base 5. I could call it quint. Now, would it make any sense for hex to be
a method while quint is an external function? Or should I modify the Python
core to also make quint a method?
> id
id applies to all objects, including methods. Do you really want
1.id().id().id() to be legal Python?
> int
Constructor function.
> len
This makes sense as a method, somewhat.
> list
Constructor function. If a write a new sequence class called "linked_list",
should I modify all existing sequence types to have a linked_list method?
> long
Constructor.
> map
As method of what? The function? The sequence? Which one?
> max
> min
1.max(2) # Evaluates to 2
1.min(2) # Evalutes to 1
I rest my case.
> oct
See hex.
> ord
This would make sense as a method, somewhat.
> pow
This is just a shortcut for the power operator (a ** b). The power operator
is overloaded like any other operator (__pow__ for class instances).
> reduce
See: map
> repr
Applies to all objects. repr has a default behavior for class instances,
which can be modified by defining __repr__. If this was a method, there
could be no default behavior.
> round
This might make sense as a method.
> str
See: repr
> tuple
Constructor.
> unichr
Do you really want to change the external interface of integers just because
we have a new string type?
> vars
See: dir
--
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