Method Underscores?

J

John Roth

Chris S. said:
Is there a purpose for using trailing and leading double underscores for
built-in method names? My impression was that underscores are supposed to
imply some sort of pseudo-privatization, but would using myclass.len()
instead of myclass.__len__() really cause Python considerable harm? As
much as I adore Python, I have to admit, I find this to be one of the
language's most "unPythonic" features and a key arguing point against
Python. I've searched for a discussion on this topic in the groups
archives, but found little. What are everyone's thoughts on this subject?

Languages that make everything methods tend to have
two characteristics in common: a single base class, and
no facility for procedural programming.

Python does not have a single base class, and one of its
strong points is that it's a multi-paradigm language. You can
use it for procedural programming without having to put
everything in the object paradigm. People quite frequently
do this for scripts.

In single base class languages, the top object in the
hierarchy is a hod-podge of methods that have no
relationship other than the language designer's decision
that the function is fundamental enough that it needs to
be available to every object, whether it needs it or not.

It would be technically possible (and relatively easy for
anyone with a bit of experience with working on Python's
internals) to add, for example, .len() to the object class.
Would this break anything? Unlikely in most cases
(there is one edge case I know of where it would). Alex Martelli's
comment elsewhere in this thread assumes one would also
remove it from the builtins, which would of course break
everything in sight.

Removing it from the builtins is equivalent to eliminating
the multi-paradigm nature of Python: you could no longer
do effective procedural programming without the builtins!
This is simply not going to happen: it would be a completely
different language. Discussing it is futile.

Also, the new .len() method would only be available
to new style classes written in Python, and then only
if the author of some subclass had not decided to implement
their own len() method with different semantics. The
first of these two objections goes away in Python 3.0,
where old style classes are eliminated. The second,
however, is a difficulty that all "pure" OO languages
need to deal with, and there is no real good way of
handling it (meaning obvious, works all the time
everywhere, and upward compatible without breaking
anything.).

Having a parallel structure where a large number of
builtins are also implemented as methods on object
violates Python's basic philosophy in at least two
ways: there should only be one (obvious) way to do
something, and things should work the same way
everywhere.

All that said, I'd like to see some of the builtins
as methods on object, but since it's not going
to happen, it's not worth worrying about.

John Roth
 
J

Jeremy Bowers

Forth. Postscript.

I guess I should have specified "general purpose". Yeah, of course you can
do anything in Forth, Postscript, or Applescript, but they aren't much
competition for Python.

(By which I mean they serve vastly differing uses; I've never used Forth
directly but I have done HP-48 programming and I liked it and I hear
there's a lot of similarities. Great calculator language. Of the ones
listed Forth is the closest to being a plausible app language, but it
would need more libraries.)
 
A

Alex Martelli

Jeremy Bowers said:
Point. Maybe Cobol on the same theory? I don't know, I've never used
either.

Roughly, I guess. But a typical Applescript might be:

tell application "Microsoft Word"
open "MyWordFile"
set selection to paragraph 1
data size of selection as string
end tell

while a typical Cobol would start

IDENTIFICATION DIVISION.
PROGRAM-ID. My-Program.
AUTHOR. Some Body.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 9 VALUE ZEROS.

etc, etc; more full-stops, typically.

I guess if you're so stuck on the double-underscore-is-too-much-
punctuation idea, you *deserve* to try to do all your programming in a
combo of COBOL and Applescript :)

I'm thinking I'll stick with Python.

Me, too. But I understand how the "antiperl" (lrep?-) nearly
punctuation free style of Applescript may be tempting -- if Applescript
were cross-platform, it might perhaps make an even better beginners'
language than Python (and I think that of few languages). It doesn't
scale up as well as Python, though, it appears to me.


Alex
 
V

Ville Vainio

Lonnie> It's extremely common for Python newbies to accidentally
Lonnie> overwrite the names of important things. I see stuff like
Lonnie> this all the time:

Lonnie> list = [1,2,3]
Lonnie> str = "Hello world"

This is not really a mistake. If you don't use "list" builtin in the
same function, it's ok to override it. Not so on module level, of
course.
 
S

Steve Holden

Alex said:
[...]
Roughly, I guess. But a typical Applescript might be:

tell application "Microsoft Word"
open "MyWordFile"
set selection to paragraph 1
data size of selection as string
end tell
[...]
I guess if you're so stuck on the double-underscore-is-too-much-
punctuation idea, you *deserve* to try to do all your programming in a
combo of COBOL and Applescript :)
What a dreadful; fate to wish on anybody!
Me, too. But I understand how the "antiperl" (lrep?-) nearly
punctuation free style of Applescript may be tempting -- if Applescript
were cross-platform, it might perhaps make an even better beginners'
language than Python (and I think that of few languages). It doesn't
scale up as well as Python, though, it appears to me.
Yeah, it does have that Logo-like quality about it, doesn't it? For
beginners, of course, it's typically more interesting to be able to
write scripts to get their computers to do things they'd otherwise have
to do themselves, so AppleScript-for-Windows might be a good Python project!

regards
Steve
 
A

Alex Martelli

Steve Holden said:
Yeah, it does have that Logo-like quality about it, doesn't it? For
beginners, of course, it's typically more interesting to be able to
write scripts to get their computers to do things they'd otherwise have
to do themselves, so AppleScript-for-Windows might be a good Python project!

Yes, but mapping the simple "apple events" on which applescript is based
(and which are widespread in applications as well as the OS on the Mac,
e.g. MS Office apps implement them) to COM's richer and more complex
ways (and COM/Automation enjoys a similar prominence on Windows and its
applications) sounds like quite a task, requiring deep understanding of
both platforms. Maybe I'm being pessimistic...


Alex
 
H

has

Applescript, maybe. 'tell foo of bar to tweak zippo' where python would
have bar.foo.tweak(zippo), looks like. (I'm not enthusiastic about the
idea, just pointing it out!-).

Trust me, it's not a place you want to go. ;p
 
H

has

Even better idea: just use Logo! :)

Seriously, the AppleScript language is no great shakes. The only
things going for it are that AppleScript code is easy for
non-programmers to read, and that it's got very good Apple Event
Manager and Open Scripting Architecture support built in. Then again,
AppleScript code is a right pain to write, and the only reason it
remains the popular choice for Mac application scripting is because
most every other scripting language has failed to provide AEM and OSA
support worth a damn. :(

Yes, but mapping the simple "apple events" on which applescript is based
(and which are widespread in applications as well as the OS on the Mac,
e.g. MS Office apps implement them) to COM's richer and more complex
ways (and COM/Automation enjoys a similar prominence on Windows and its
applications) sounds like quite a task, requiring deep understanding of
both platforms. Maybe I'm being pessimistic...

Any fule could implement a clone of the AS _language_; there's
actually very little to it. Heck, you could probably write a parser
that compiles an AppleScript-like syntax into Python source code
fairly easily. It's the OSA component and IPC stuff that would take
time. The former you could probably do fairly easily using COM, but
for IPC support you'll find Windows' DCOM/WSH to be a VERY different
beast to Apple events, which is basically synchronous/asynchronous RPC
plus basic relational queries. This means that almost all the hard
work to support a _full_ AppleScript clone would be on the
application, not the language, side. Which means not only providing
application developers with suitable frameworks but also persuading
them to use them - no small task to say the least.

An application's Apple event interface is basically a whole additional
View-Controller layer - a peer to its GUI/CLI/web/etc. interface(s) -
built atop the Apple Event Manager/Cocoa Scripting framework, just as
its GUI interface is built with the Application Kit framework. And,
just as there's a whole bunch of rules and guidelines on how GUI
interfaces should look and behave, there's an equivalent for scripting
interface developers. e.g. See Apple's Scripting Interface Guidelines,
a partner to its GUI-oriented HIG,
at<http://developer.apple.com/technotes/tn2002/tn2106.html>.

It's deceptively powerful stuff, though a lot of applications don't
support it nearly as well as they could - or should (many of Apple's
own apps are guilty here). Mac Office apps aren't really a good
example - their Apple event interfaces are really just a thin wrapper
around their VB APIs and quite atypical. Take a look at something like
Adobe InDesign (one of their engineers is Jon Pugh, one of the
original Apple team behind OSA, etc.) - it has very comprehensive and
well designed AE interface, as well as JavaScript and VB APIs.


You probably could implement a Windows clone of the Apple Event
Manager using DCOM or whatever as your inter-process message transport
mechanism, but it looks as if Longhorn will have something fairly
similar to Apple events and the Apple Event Object Model with its
Indigo and OPath technologies, so probably not worth doing now. (Makes
you wonder what might've been had OpenDoc taken off though - it was
built on Apple events and, IIRC, intended to be cross-platform, and
years ahead of its time.)

Mind you, I've already implemented Python libraries for constructing
object specifier-based queries
<http://freespace.virgin.net/hamish.sanderson/appscript.html> and am
working on supporting the Apple Event Object Model in Python, so you
probably could port these to other systems just by replacing the
platform-specific Carbon.AE extensions used for the inter-process
messaging part with XML-RPC, dBus or whatever else is to hand.


Happy to discuss this stuff further if anyone's interested and wants
to take it to a separate thread.

has
 
H

has

Me, too. But I understand how the "antiperl" (lrep?-) nearly
punctuation free style of Applescript may be tempting -- if Applescript
were cross-platform, it might perhaps make an even better beginners'
language than Python (and I think that of few languages).

Ahhhhh, don't say that!

While AppleScript code can be wonderfully readable even to folk with
absolutely no knowledge of the language, it's often an absolute swine
to write - stripping out all the punctuation and allowing applications
and osaxen (extensions) to add their own arbitrary keywords leaves the
language semantics appallingly ambiguous and confused.

The right approach, imo, would be to design a very conventional
scripting language, perhaps using a S-expression based [non-]syntax
that's easy to manipulate programmatically, and leave all the fancy
syntax, visual effects, beginner-friendly dumbing down, etc. to the
editing tools, which are much better placed to know what's really what
than any dumb text-based source code can ever be. Still a major
undertaking, but a better chance of pulling it off than.

It doesn't scale up as well as Python, though, it appears to me.

Heh, more like AppleScript doesn't scale *at all*. :p

has
 
D

Dave Benjamin

Wow. To me, the double-underscores is one of the most Pythonic things I can
think of. I don't believe that "Pythonic" means "good", at least not in my
book. But it's definitely one of Python's birthmarks.

I think len() is really cool. It's short, it's generic, and it's consistent.
It's not object oriented, of course. I don't believe that "object oriented"
means "good" either. But then, the OO way doesn't really make sense anyway.
Why would you send the list a "size" message, and have it respond to you,
"5"? That's silly. Lists don't talk. You want the size, you "take" it.
len(L) says "take the length of 'L'". Now, that's better.

The other great thing about len() is that it's the same for all collections.
Sure, there's nothing to stop you from implementing ".len()", or ".size()",
or ".numberOfElementsInsideOfMe()", but why would you? You've already
learned Python, you know that "len" takes the length of something, and you
certainly don't want to confuse yourself. Why not just implement the
protocol? What's it called? Of course... this is Python... must be those
double-underscores, right?

Contrast this with Java's ".size()", ".length()", ".length", etc. How would
you write a generic function to consolidate them all? You'd have to write
one overloaded method to take a Collection, another to take an Object[], and
so on down the line. If the class has an interface, you're lucky. Otherwise,
it's time to use reflection. Bleah. I'll take Python over this any day.

Although Ruby, I think, gets this right, so not all OO implementations are
flawed in this respect.

Languages that make everything methods tend to have
two characteristics in common: a single base class, and
no facility for procedural programming.

The only languages I could think of that match this criteria are Ruby,
Smalltalk, and Java. Are there others I've missed?
Python does not have a single base class, and one of its
strong points is that it's a multi-paradigm language. You can
use it for procedural programming without having to put
everything in the object paradigm. People quite frequently
do this for scripts.

Exactly. And a lot of people learn Python starting with algebra (I know
several non-Python programmers that use Python as a desk calculator), and
then move on to procedures, and then finally master objects. Python lets you
get pretty far without having to send messages to thingies. You can rely on
things you probably already know, like math. One of the things that I love
about Python is that it doesn't try to be a pure-OO language. It does OO,
and does it rather well, I think, but sometimes you just have to write a
couple of functions and run, and Python acknowledges that.
In single base class languages, the top object in the
hierarchy is a hod-podge of methods that have no
relationship other than the language designer's decision
that the function is fundamental enough that it needs to
be available to every object, whether it needs it or not.

It depends on the language. Java's Object has a pretty small number of
methods, although some of them seem rather inappropriate. I don't know if
it's fair to call Java a single base class language, though, since it has
types that support operators and not methods (and Arrays, which fall
somewhere in between). In any case, I recall proclaiming to a Java-loving
coworker one day, "Object should have a 'length' method!". He thought it was
the dumbest idea ever, but the reason I mentioned it is that I was thinking
to myself, "Gee, it's so nice that I have one way to get the length of
anything in Python. If Java only put a 'length' method on Object from the
beginning, it probably would have been more consistent." The only other way
I could see pulling this off would be to make an interface for everything
Lengthable, and hope people remember to implement it, or switch to a
structural typing system and get the statically-typed version of what Python
already does easily.
It would be technically possible (and relatively easy for
anyone with a bit of experience with working on Python's
internals) to add, for example, .len() to the object class.
Would this break anything? Unlikely in most cases
(there is one edge case I know of where it would). Alex Martelli's
comment elsewhere in this thread assumes one would also
remove it from the builtins, which would of course break
everything in sight.

And if you don't remove it, then TMTOWTDI, which means that Guido will turn
into a pie and we will all lose our minds and $t@rt t@lk1ng l1k3 th1s!11!!!
Removing it from the builtins is equivalent to eliminating
the multi-paradigm nature of Python: you could no longer
do effective procedural programming without the builtins!
This is simply not going to happen: it would be a completely
different language. Discussing it is futile.

The builtins are great. Every language should have a basis library. To do
otherwise is to overengineer, to favor namespace elegance over practical
usefulness, and to demote functions and procedures to being second-class
citizens. The basis is what gives a language its character. It's like slang.
Nobody likes a society with no slang at all. We want a slang we like. And
Python's got a pretty good one, I think.
Also, the new .len() method would only be available
to new style classes written in Python, and then only
if the author of some subclass had not decided to implement
their own len() method with different semantics. The
first of these two objections goes away in Python 3.0,
where old style classes are eliminated. The second,
however, is a difficulty that all "pure" OO languages
need to deal with, and there is no real good way of
handling it (meaning obvious, works all the time
everywhere, and upward compatible without breaking
anything.).

IOW, a complete waste of time.
Having a parallel structure where a large number of
builtins are also implemented as methods on object
violates Python's basic philosophy in at least two
ways: there should only be one (obvious) way to do
something, and things should work the same way
everywhere.

Well, technically, you can call L.__len__() today, so there are two ways to
do it. You could also write a loop with a counter, or turn it into a string
and count the commas. But none of these are *obvious* ways, and I think that
the *obvious* is what makes that "law" really mean something. To a Java
programmer, "len()" may not be obvious, but once you learn it, it really
can't get any more obvious. I remember my delight when PythonWin
automatically supported "len()" on COM collections, and my dismay (and
motivation to fix this deficiency immediately) when I discovered that
Jython+Jintegra didn't. In COM-land, they say ".Count()", and they probably
don't have an interface for it either. ;)
All that said, I'd like to see some of the builtins
as methods on object, but since it's not going
to happen, it's not worth worrying about.

Really? Which ones?

I'd like to see map and filter be built into sequences, and I suppose "id()"
would make sense on Object, but I really can't think of any others I'd change.
 
P

Peter Maas

Hans said:
I don't know what "truly OO" is. It appears that every language
designer has his own ideas about that. Hence, Java's OO is not the same
as C++'s, or Smalltalk's, or Self's, or CLOS's, or...

I once read the following definition:

An OO language must support:

- creating and using objects
- encapsulation (data hiding)
- inheritance
- polymorphism

Current Python fulfills this. Don't know about pre 2.2.versions.

Mit freundlichen Gruessen,

Peter Maas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top