Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Double underscores -- ugly?

38 views
Skip to first unread message

benhoyt

unread,
Feb 18, 2008, 5:28:43 PM2/18/08
to
Hi guys,

I've been using Python for some time now, and am very impressed with
its lack of red tape and its clean syntax -- both probably due to the
BDFL's ability to know when to say "no".

Most of the things that "got me" initially have been addressed in
recent versions of Python, or are being addressed in Python 3000. But
it looks like the double underscores are staying as is. This is
probably a good thing unless there are better alternatives, but ...

Is it just me that thinks "__init__" is rather ugly? Not to mention
"if __name__ == '__main__': ..."?

I realise that double underscores make the language conceptually
cleaner in many ways (because fancy syntax and operator overloading
are just handled by methods), but they don't *look* nice.

A solution could be as simple as syntactic sugar that converted to
double underscores behind the scenes. A couple of ideas that come to
my mind (though these have their problems too):

def ~init(self): # shows it's special, but too like a C++ destructor
def +init(self): # a bit too additive :-)
defop add(self, other): # or this, equivalent to "def __add__"
def operator add(self, other): # new keyword, and a bit wordy

Has anyone thought about alternatives? Is there a previous discussion
on this I can look up?

Cheers,
Ben.

Ben Finney

unread,
Feb 18, 2008, 6:39:10 PM2/18/08
to
benhoyt <ben...@gmail.com> writes:

> I realise that double underscores make the language conceptually
> cleaner in many ways (because fancy syntax and operator overloading
> are just handled by methods), but they don't *look* nice.

That's a good thing, in that it draws attention to the names. The
convention is by design: these names will be treated specially, so
they should stand out visually to the reader.

> A solution could be as simple as syntactic sugar that converted to
> double underscores behind the scenes. A couple of ideas that come to
> my mind (though these have their problems too):
>
> def ~init(self): # shows it's special, but too like a C++ destructor
> def +init(self): # a bit too additive :-)
> defop add(self, other): # or this, equivalent to "def __add__"
> def operator add(self, other): # new keyword, and a bit wordy

None of these, IMO, meet the "needs to stand out" requirement met by
double-underscore names.

They also introduce special cases for the language parser (and thus
for the reader to understand how the language will be parsed), whereas
double-underscore names work without any special syntax handling.

--
\ “Holy contributing to the delinquency of minors, Batman!” |
`\ —Robin |
_o__) |
Ben Finney

Berwyn

unread,
Feb 18, 2008, 7:14:32 PM2/18/08
to ben...@gmail.com
> Is it just me that thinks "__init__" is rather ugly? Not to mention
> "if __name__ == '__main__': ..."?

That ugliness has long been my biggest bugbear with python, too. The
__name__ == '__main__' thing is something I always have to look up,
every time I use it, too ... awkward.

I'd settle for:

hidden def init(self): # which could be extended to work
for everything "hidden x=3"
...

And for __name__ == '__main__' how about:

if sys.main():
...

Ben Finney

unread,
Feb 18, 2008, 7:33:26 PM2/18/08
to
benhoyt <ben...@gmail.com> writes:

> Not to mention "if __name__ == '__main__': ..."?

Unlike the double-underscore attribute names for signalling "special
meaning", that particular hack is IMO unnecessarily ugly.

I don't, however, think it's likely to go away any time soon. If
that's the ugliest convention people can find in Python (as opposed to
the limitless *non*-conventional ugliness that programmers are capable
of in any language), then Python is doing pretty well.

--
\ “An idea isn't responsible for the people who believe in it.” |
`\ —Donald Robert Perry Marquis |
_o__) |
Ben Finney

Asun Friere

unread,
Feb 18, 2008, 8:36:49 PM2/18/08
to

benhoyt wrote:
> Is it just me that thinks "__init__" is rather ugly?

I used to hate looking at and having the type out all those
underscores (surely two leading or one on either side would do?), but
I've gotten so used to it by now the eyes don't see and the fingers
work by themselves.

> Not to mention
> "if __name__ == '__main__': ..."?

Which ugliness is only trumped by the use of 'main' as a function name
thus:

if __name__ == '__main__' : main()

Terry Reedy

unread,
Feb 18, 2008, 9:23:29 PM2/18/08
to pytho...@python.org

"benhoyt" <ben...@gmail.com> wrote in message
news:a52fa343-1b7a-4e99...@i7g2000prf.googlegroups.com...

| Hi guys,
|
| I've been using Python for some time now, and am very impressed with
| its lack of red tape and its clean syntax -- both probably due to the
| BDFL's ability to know when to say "no".
|
| Most of the things that "got me" initially have been addressed in
| recent versions of Python, or are being addressed in Python 3000. But
| it looks like the double underscores are staying as is. This is
| probably a good thing unless there are better alternatives, but ...
|
| Is it just me that thinks "__init__" is rather ugly?

No, the reservered special names are supposed to be ugly ;-) -- or at least
to stand out. However, since special methods are almost always called
indirectly by syntax and not directly, only the class writer or reader, but
not users, generally see them.

| Not to mention "if __name__ == '__main__': ..."?

Someone (perhaps me) once suggested on pydev using 'main' instead, but a
couple of people piped back that they regularly name their main module (as
opposed to the startup script) 'main'. So much for that idea. 'main__'
might not look as bad, but anything other that '__main__' introduces an
inconsistency with the reserved name rule. Changing '__name__' has the
same pair of problems (conflict with user names and consistency). So I
decided to live with the current incantation.

Terry Jan Reedy

|

Raymond Hettinger

unread,
Feb 18, 2008, 10:17:26 PM2/18/08
to
[benhoyt]

> Is it just me that thinks "__init__" is rather ugly?

I also find it unattractive and unpleasant to type.

In Py3.0, I would support a single underscore convention, _init_ or
somesuch.

I'm not sure what the aesthetic reasons are, but somehow the change
from double underscores to single underscores makes the result a lot
less offensive to my eyes.

Raymond

benhoyt

unread,
Feb 19, 2008, 12:39:23 AM2/19/08
to

> [Terry Jan Reedy]

> No, the reservered special names are supposed to be ugly ;-) -- or at least
> to stand out. However, since special methods are almost always called
> indirectly by syntax and not directly, only the class writer or reader, but
> not users, generally see them.

Fair enough, but my problem is that class writers are users too. :-)

-Ben

Duncan Booth

unread,
Feb 19, 2008, 4:01:18 AM2/19/08
to
Berwyn <ber...@gmail.com> wrote:

Or even:

@hidden
def init(self): ...

@main
def mymainfunc():
...


The first of those probably wants some metaclass support to make it work
cleanly, but here's a sample implementation for the second one:

import sys, atexit
def main(f):
"""Decorator for main function"""
def runner():
sys.exit(f())
if f.func_globals['__name__']=='__main__':
atexit.register(runner)
return f

print "define mymainfunc"
@main
def mymainfunc(args=sys.argv):
print "Got args", args
return 3
print "end of script"

If you have multiple functions marked as main that will run them in
reverse order, so it might be better to put them on a list and use a
single runner to clear the list. Also, I have no idea what happens to
the exit code if you use this decorator more than once.

BTW, should anyone be wondering, you can still use atexit inside a
function called from atexit and any registered functions are then called
when the first one returns.

Marco Mariani

unread,
Feb 19, 2008, 9:17:14 AM2/19/08
to
Ben Finney wrote:

>> I realise that double underscores make the language conceptually
>> cleaner in many ways (because fancy syntax and operator overloading
>> are just handled by methods), but they don't *look* nice.
>
> That's a good thing, in that it draws attention to the names.

Well, double underscore is awful when you have to read code with the
wrong typeface, possibly printed.

Jason

unread,
Feb 19, 2008, 9:37:13 AM2/19/08
to

Hmm. I must be the only person who doesn't think the double
underscores are ugly. To me, they seem to provide plenty of attention
to the special methods, but still appear clean due to their almost
white-space-like nature. Given the use of underscores to indicate
italics in plain-text, the special methods seem (to me) to have extra
emphasis.

I don't print my code often, so that's a caveat, and I came from a C/C+
+ background.

I agree with Ben, that your suggestions don't particularly stand out.
They might stand out if the editor you used supported syntax
highlighting. Personally, I find the examples with the plus and tilde
(+, ~) to be more noisy and ugly than the underscores.

Think of the underscores as a serene white-space day, with a simple
black road that takes you to the special name. Or, you can wonder
what I'm smoking when I code.... *grin*

--Jason

Wildemar Wildenburger

unread,
Feb 19, 2008, 11:26:53 AM2/19/08
to
Jason wrote:
> Hmm. I must be the only person who doesn't think the double
> underscores are ugly.
Nope. I like them too. :)

Frankly, I think it's just a matter of adaption. I too found it rather
"ugly" in the beginning, but with anything, I've gotten used to it. (And
I wholeheartedly support your "looks like underlined / is unintrusive
like whitespace" argument.)

/W

Hyuga

unread,
Feb 19, 2008, 12:13:40 PM2/19/08
to
On Feb 19, 4:01 am, Duncan Booth <duncan.bo...@invalid.invalid> wrote:

> Berwyn <berh...@gmail.com> wrote:
> >> Is it just me that thinks "__init__" is rather ugly? Not to mention
> >> "if __name__ == '__main__': ..."?
>
> > That ugliness has long been my biggest bugbear with python, too. The
> > __name__ == '__main__' thing is something I always have to look up,
> > every time I use it, too ... awkward.
>
> > I'd settle for:
>
> > hidden def init(self): # which could be extended to work
> > for everything "hidden x=3"
> > ...
>
> > And for __name__ == '__main__' how about:
>
> > if sys.main():
> > ...
>
> Or even:
>
> @hidden
> def init(self): ...
>
> @main
> def mymainfunc():
> ...
>

I'd much rather type a few underscores than have to constantly use
decorators. I don't see what's so ugly about __init__. To me it just
looks like it's underscored, though maybe that comes from having
worked with a lot of wikis. But I really don't find it an
encumbrance, and the fact that it requires no special handling from
the parser is just part of the beautiful simplicity of Python.

Hyuga

Ben Finney

unread,
Feb 19, 2008, 5:02:44 PM2/19/08
to
Marco Mariani <ma...@sferacarta.com> writes:

The wrong typeface can make anything awful to read. This is unrelated
to double-underscores.

The solution, therefore, is also unrelated to double-underscores:
choose an appropriate typeface.

--
\ “Courage is not the absence of fear, but the decision that |
`\ something else is more important than fear.” —Ambrose |
_o__) Redmoon |
Ben Finney

casti...@gmail.com

unread,
Feb 19, 2008, 5:25:08 PM2/19/08
to
On Feb 19, 10:26 am, Wildemar Wildenburger

My editor actually renders them as miniature chess pieces. The
bartender said she runs a pre-execution step, that searches and
replaces a double-colon with the underscores.

benhoyt

unread,
Feb 19, 2008, 9:17:45 PM2/19/08
to

> My editor actually renders [underscores] as miniature chess pieces.

> The bartender said she runs a pre-execution step, that searches and
> replaces a double-colon with the underscores.

Heh, that makes me think. Great use for character encodings! Just like
Tim Hatch's "pybraces", we could use any character combo we liked and
just implement a character encoding for it. See:
http://timhatch.com/projects/pybraces/

Just kidding.

Seriously, though, I give in. There's no perfect solution, and it's
probably just a matter of getting over it. Though I wouldn't mind
Raymond Hettinger's suggestion of using single underscores as in
_init_. Only half as ugly. :-)

Then again, what's stopping us just using a single leading underscore?
Nobody calls their own private methods _init or _add ... and that's
only 1/4 the ugliness, which is getting pretty good.

-Ben

Steve Holden

unread,
Feb 19, 2008, 9:20:28 PM2/19/08
to pytho...@python.org

If you're taking programming advice from a bartender your postings
suddenly start to make sense (though not, unfortunately, as comments
about programming). Do you think perhaps yo might be trying just a
little too hard?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Ben Finney

unread,
Feb 19, 2008, 10:54:58 PM2/19/08
to
benhoyt <ben...@gmail.com> writes:

> Then again, what's stopping us just using a single leading underscore?
> Nobody calls their own private methods _init or _add

You must be looking at different code from the rest of us. A single
leading underscore on the name *is* the convention for "this attribute
is not part of the external interface", which is about as "private" as
Python normally gets.

--
\ “God forbid that any book should be banned. The practice is |
`\ as indefensible as infanticide.” —Dame Rebecca West |
_o__) |
Ben Finney

Erik Max Francis

unread,
Feb 19, 2008, 11:56:36 PM2/19/08
to
casti...@gmail.com wrote:

> My editor actually renders them as miniature chess pieces. The
> bartender said she runs a pre-execution step, that searches and
> replaces a double-colon with the underscores.

I'm sorry, did you hit your head before dashing off your recent posts or
something?

--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
One cannot always be a hero, but one can always be a man.
-- Goethe

Ivan Illarionov

unread,
Feb 20, 2008, 12:50:33 AM2/20/08
to
I would like to see something like %init or &init to be converted to
__init__ behind the scenes. And $something to be converted to
self.something. But, unfortunately, most Python people would consider
this ugly just because Perl uses too much syntactic sugar and anything
Perl-like is considered ugly in Python community. So, unless Perl die,
I believe that Python will remain sugar-free.

Ben Finney

unread,
Feb 20, 2008, 1:09:40 AM2/20/08
to
Ivan Illarionov <ivan.il...@gmail.com> writes:

> I would like to see something like %init or &init to be converted to
> __init__ behind the scenes.

I would prefer that the names I create and see in the code are the
names actually used by the Python runtime, with no magic name
conversion "behind the scenes".

--
\ “It is far better to grasp the universe as it really is than to |
`\ persist in delusion, however satisfying and reassuring.” |
_o__) —Carl Sagan |
Ben Finney

benhoyt

unread,
Feb 20, 2008, 3:49:59 AM2/20/08
to

> > Then again, what's stopping us just using a single leading underscore?
> > Nobody calls their own private methods _init or _add
>
> You must be looking at different code from the rest of us. A single
> leading underscore on the name *is* the convention for "this attribute
> is not part of the external interface", which is about as "private" as
> Python normally gets.

Yeah, I understand that. I meant -- and I could be wrong -- that I
haven't seen people creating a method called "init" with a single
leading underscore. It's too similar to "__init__", so they would use
some other name.

In other words, defining _init to mean what __init__ now means
probably wouldn't cause name conflicts.

-Ben

Ben Finney

unread,
Feb 20, 2008, 4:11:49 AM2/20/08
to
Please preserve attribution lines on the quoted material, so we can
see who wrote what at each level.

benhoyt <ben...@gmail.com> writes:

But it would conflict with the existing conventions. '_init' as a name
indicates that it's *not* treated specially, because it's not named
with double-underscores. Since it *is* treated specially by Python, it
would be confusing not to follow the convention for naming such
attributes.

--
\ "I know you believe you understood what you think I said, but I |
`\ am not sure you realize that what you heard is not what I |
_o__) meant." -- Robert J. McCloskey |
Ben Finney

Jorge Godoy

unread,
Feb 20, 2008, 5:17:40 AM2/20/08
to
Ivan Illarionov wrote:

A good text editor allows you to replace text. Some of them can even do
that for you with macros when you save a document or open it for editing
(making it possible to go from "$" to "self" and vice-versa).

Maybe using another tool would solve your problem.

Bruno Desthuilliers

unread,
Feb 20, 2008, 5:57:37 AM2/20/08
to
Jason a écrit :
(snip)

> Hmm. I must be the only person who doesn't think the double
> underscores are ugly.

As far as I'm concerned, I just don't care if they are "ugly" or not -
FWIW, I never ever think of them in terms of "beauty" or "ugliness".

What I do care about is that they simply and clearly convey a special
meaning (just like the _single_leading_underscore names), and make sure
you won't accidentaly override such a name.

Bruno Desthuilliers

unread,
Feb 20, 2008, 5:59:02 AM2/20/08
to
benhoyt a écrit :
(snip)

> Then again, what's stopping us just using a single leading underscore?
> Nobody calls their own private methods _init or _add

I do.

Bruno Desthuilliers

unread,
Feb 20, 2008, 6:02:46 AM2/20/08
to
benhoyt a écrit :

>>> Then again, what's stopping us just using a single leading underscore?
>>> Nobody calls their own private methods _init or _add
>> You must be looking at different code from the rest of us. A single
>> leading underscore on the name *is* the convention for "this attribute
>> is not part of the external interface", which is about as "private" as
>> Python normally gets.
>
> Yeah, I understand that. I meant -- and I could be wrong -- that I
> haven't seen people creating a method called "init" with a single
> leading underscore.

Can you see me ?

> It's too similar to "__init__", so they would use
> some other name.

Nope. It's perfect for a template method that is supposed to be called
from the parent's class __init__.

> In other words, defining _init to mean what __init__ now means
> probably wouldn't cause name conflicts.

It would.

What you obviously fail to understand is that the __magic_name__
convention is here to clearly separate language implementation space
from user space. Your suggestion would break this.

cokof...@gmail.com

unread,
Feb 20, 2008, 6:12:39 AM2/20/08
to
So people's problem with __word__ is that it is not very readable?

How so, it stands out on page, it clearly is different from other
objects and doesn't abuse other symbols that generally have a meaning
based on their use.

I haven't seen a single alternative that really stands out as much as
__word__ does.

Ben Finney

unread,
Feb 20, 2008, 6:24:50 AM2/20/08
to
cokof...@gmail.com writes:

> So people's problem with __word__ is that it is not very readable?

No, the complaint seems to be that it's "ugly".

--
\ "[T]he speed of response of the internet will re-introduce us |
`\ to that from which our political systems have separated us for |
_o__) so long, the consequences of our own actions." -- Douglas Adams |
Ben Finney

Steven D'Aprano

unread,
Feb 20, 2008, 7:09:26 AM2/20/08
to

My only two gripes about double underscore names are that:

(1) My news reader interprets _X_ as "underline X", which leads to ugly
display. I could turn that feature off, but that would mean losing *X*
"bold X", which I like.

(2) Two underscores __ is insufficiently visually different from a single
underscore _. Choosing a good font reduces the problem, but doesn't make
it go away.

However, these are minor gripes. On a scale of 0 to -9, where 0 is "it
doesn't upset me or please me at all" and -9 is "I'd rather die than live
with this one more minute", these gripes are both about a -2, and far out-
weighed by the advantages. Taking the advantages and the disadvantages
both into account, I give it a total score of about +4.

--
Steven

Steven D'Aprano

unread,
Feb 20, 2008, 7:25:33 AM2/20/08
to
On Tue, 19 Feb 2008 21:50:33 -0800, Ivan Illarionov wrote:

> I would like to see something like %init or &init to be converted to
> __init__ behind the scenes.

Unfortunately -- or perhaps fortunately -- % clashes with the already
established uses of % as the modulus operator for numbers and the
interpolation operator for strings.

>>> 5 % 3
2
>>> "hello %s" % "world"
'hello world'


The last thing I want is to have to study code carefully to try to work
out whether %name means __name__ or %name.

Similarly for &, the bitwise and operator:

>>> 9 & 12
8

> And $something to be converted to self.something.

Well, at least $ is unused in Python at the moment. That's a point in the
idea's favour. It's a tiny point, but a point.


> But, unfortunately, most Python people would consider
> this ugly

Yes.

> just because Perl uses too much syntactic sugar

No. It's because Perl uses too much *line noise*, punctuation characters
instead of syntax.


> and anything
> Perl-like is considered ugly in Python community. So, unless Perl die, I
> believe that Python will remain sugar-free.

Python has lots of syntactic sugar. Some examples:

"class Foo" is syntactic sugar for a call to the new.classobj() function.

Both "def foo():" and "lambda :" are syntactic sugar for a call to the
new.function() function.

Double-underscore method names are syntactic sugar.

Decorators are syntactic sugar.

Generator expressions and list comprehensions are syntactic sugar.

Dict, tuple and list literals {a: b} (a, b) [a, b] are syntactic sugar.

Method calls and method definitions are syntactic sugar.

The trinary if operator "x if flag else y" is syntactic sugar.

Most of these have been part of Python since the earliest days. Some of
them, like method calls, are so established in Python and other languages
that it's hard to remember that they could be done another way, without
syntax support, but of course they could be:

# instance.method(arg)
getattr(instance, 'method')(arg)


Python uses relatively little punctuation. That's one of the strengths of
the language, and why its very unlikely that your suggestions will be
implemented in Python.

--
Steven

casti...@gmail.com

unread,
Feb 20, 2008, 9:31:01 PM2/20/08
to
On Feb 19, 8:20 pm, Steve Holden <st...@holdenweb.com> wrote:
> Holden Web LLC              http://www.holdenweb.com/- Hide quoted text -
>
> - Show quoted text -

It's definitely possible. I've been hacking the code for some time,
and so far, the furthest I've gotten is:

>>> bartender.think()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Bartender' object has no attribute 'think'
>>>

Any ideas?

cokof...@gmail.com

unread,
Feb 21, 2008, 3:41:57 AM2/21/08
to
> > Holden Web LLC http://www.holdenweb.com/-Hide quoted text -

>
> > - Show quoted text -
>
> It's definitely possible. I've been hacking the code for some time,
> and so far, the furthest I've gotten is:
>
> >>> bartender.think()
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: 'Bartender' object has no attribute 'think'
>
>
>
> Any ideas?

You need to pass it a parameter for .drink() which in turn calls
the .pay() function, before it can .think()

grfla...@gmail.com

unread,
Feb 21, 2008, 4:39:26 AM2/21/08
to
On Feb 19, 10:01 am, Duncan Booth <duncan.bo...@invalid.invalid>
wrote:

I liked the idea, thanks. But I also wanted to be able to pass options
to the decorator, for example optparser.OptionParser instances, so
(after wrestling with nested functions for half a day!) I ended up
with the class below, FWIW.

Getting the interplay between __new__, __init__ and __call__ was
somewhat trial-and-error, and maybe I just painted myself into a
corner, but (slightly academic question) is it the case that if you
want to be able to use a decorator both with and without options, ie.
like this:

@mainmethod
def main(...)

and like this:

@mainmethod(parser=myparser)
def main(...)

then you cannot use that decorator for a function that expects or
allows a function as its first argument? Because how and when can you
decide whether that function is the decorated one or the function
parameter?

Anyway, thanks again.

[code]

class mainmethod(object):

_parser = None
kwargs = {}
args =()

def _getparser(self):
if self._parser is None:
self._parser = CommonOptionParser()
return self._parser
parser = property(_getparser)

def __new__(cls, *args, **kw):
obj = super(mainmethod, cls).__new__(cls, *args, **kw)
if len(args) == 1 and inspect.isfunction(args[0]):
#we assume that the decorator has been declared with no
arguments,
#so go to straight to __call__, don't need __init__
#if it's the case that the wrapped 'main' method allows or
#expects a function as its first (and only) positional
argument
#then you can't use this decorator
return obj(args[0])
else:
return obj

def __init__(self, *args, **kw):
self.args = args
self._parser = kw.pop('parser', None)
self.kwargs = kw

def _updatekwargs(self, dict):
#don't want default null values of parser to overwrite
anything
#passed to `mainmethod` itself
for k,v in dict.iteritems():
#can't do 'if v: ...' because empty configobj evaluates
False
if v is None or v == '':
continue
self.kwargs[k] = v

def exit(self):
try:
log.end()
except:
pass

def run(self):
options, args = self.parser.parse_args()
#the following so that command line options are made available
#to the decorated function as **kwargs
self._updatekwargs(self.parser.values.__dict__)
logargs = (
self.kwargs.get(OPTLOGFILE, None),
self.kwargs.get(OPTLOGDIR, None),
self.kwargs.get(OPTLOGPREFIX, ''),
)
self.kwargs[OPTLOGFILE] = logstart(*logargs)
log.info("SCRIPT: " + sys.argv[0])
conf = self.kwargs.get(OPTCONFIG, None)
if conf:
log.info("%s: %s" % (OPTCONFIG.upper(), conf.filename))
for k,v in self.kwargs.iteritems():
if v and k not in COMMONOPTS:
log.info("%s = %s" % (k, v))
log.divider()
return sys.exit(self.func(*self.args, **self.kwargs))

def __call__(self, f):


if f.func_globals['__name__'] == '__main__':

self.func = f
import atexit
atexit.register(self.exit)
atexit.register(self.run)
return f

[/code]

Gerard

benhoyt

unread,
Feb 21, 2008, 2:37:15 PM2/21/08
to

> Has anyone thought about alternatives? Is there a previous discussion
> on this I can look up?

Okay, I just emailed the BDFL and asked if he could tell me the origin
of the double underscore syntax for __special__ methods, and what he
said I'm pretty sure he won't mind me posting here:

> [Guido van Rossum said:]
> The specific naming convention was borrowed from the C standard, which
> reserves names like __FILE__ and __LINE__. The reason for needing a
> convention for "system-assigned" names was that I didn't want users to
> be surprised by the system giving a special meaning to methods or
> variables they had defined without intending that special meaning,
> while at the same time not wanting to introduce a physically separate
> namespace (e.g. a separate dict) for system names. I have no regrets.

After that and this thread, I'm pretty good with it, I guess. :-)

Cheers,
Ben.

casti...@gmail.com

unread,
Feb 23, 2008, 3:32:59 PM2/23/08
to
> @mainmethod
> def main(...)
>
> and like this:
>
> @mainmethod(parser=myparser)
> def main(...)
>
> then you cannot use that decorator for a function that expects or
> allows a function as its first argument? Because how and

If it's called with only one non-keyword parameter, then the language
might have done it; if not, guaranteed not. (More succintly, if it's
not, the language didn't.)

So, to get:
> @mainmethod
> def main(...)
you'll have to check the parameter counts, and you can never invoke
with just one callable parameter, pretty easy; bulky; your choice
between:

@mainmethod()
def main(...)

Note: 'Non-keyword' -and- callable!

0 new messages