Method Underscores?

Andrew Dalke adalke at mindspring.com
Thu Oct 21 10:51:27 CEST 2004


Chris S. wrote:
 > Using method(object) instead of object.method() is
 > a throwback from Python's earlier non-object oriented days,

Pardon?  It's been O-O since at least version 0.9 over
10 years ago.

 > Personally, I'd
> like to see the use of underscores in name-mangling thrown out 
> altogether, as they "uglify" certain code and have no practical use in a 
> truly OO language, but I'm sure that's a point many will disagree with 
> me on.

I and others have made some comments already, but let me add
one more.

Given a C++ viewpoint, one way to think of Python programming
is that it's like template based programming.  The Python
function

def sum(data):
   x = iter(data)
   tot = x.next()
   for item in x:
     tot += item
   return tot

works on any data container so long as that container
can be iterated and its values can be summed.  (And the
container must have at least one item.)

 >>> sum(["This", "is", "a", "test"])
'Thisisatest'
 >>>

The way to do that in C++ is with a template.  I tried
to figure out how to do it but my C++ skills are about
8 years rusted.  Perhaps something like  .... ?

template <typename Container, typename T>
T sum(Container container) {
   T val;
   for (Container::const_iterator it = container.first();
        it != container.last(); ++it) {
     val += *it;
   }
   return val;
}

You would not do it using OO containers because then
you're left with Java-style casting everywhere.  (Sun
has added generics in the most recent version of Java.
See Bruce Eckel's commentary on the topic at
   http://www.mindview.net/WebLog )


If you accept that Python program has a strong template
aspect, in addition to O-O programming, then the builtins
like 'abs', 'cmp', etc. can be seen as generic algorithms
and not methods of a given data type.

Granted, some of the algorithms are trivial, as for
abs(), but the 'cmp()' algorithm is quite involved.

That means your statement, that these functions "have
no practical use in a truly OO language" is not applicable,
because Python is a mixed OO/imperative/template-based
programming language.

				Andrew
				dalke at dalkescientific.com



More information about the Python-list mailing list