473,469 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Double underscores -- ugly?

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.

Feb 18 '08 #1
20 4171
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():
...
Feb 19 '08 #2
benhoyt <be*****@gmail.comwrites:
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
Feb 19 '08 #3

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()
Feb 19 '08 #4
[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
Feb 19 '08 #5
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.
Feb 19 '08 #6
On Feb 18, 3:28 pm, benhoyt <benh...@gmail.comwrote:
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.
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
Feb 19 '08 #7
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
Feb 19 '08 #8
Marco Mariani <ma***@sferacarta.comwrites:
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.
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
Feb 19 '08 #9
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
Feb 20 '08 #10
benhoyt <be*****@gmail.comwrites:
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
Feb 20 '08 #11
ca********@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 && ma*@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
Feb 20 '08 #12
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.
Feb 20 '08 #13
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
Feb 20 '08 #14
Please preserve attribution lines on the quoted material, so we can
see who wrote what at each level.

benhoyt <be*****@gmail.comwrites:
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.
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
Feb 20 '08 #15
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.

Feb 20 '08 #16
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.

Feb 20 '08 #17
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.
Feb 20 '08 #18
co*********@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
Feb 20 '08 #19
On Wed, 20 Feb 2008 03:12:39 -0800, cokofreedom wrote:
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.
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
Feb 20 '08 #20
@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!
Feb 23 '08 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Chris S. | last post by:
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...
22
by: BRIAN VICKERY | last post by:
I've heard that some compilers reserve the '_' starting a symbol for system variables/symbols. I can not find this documented anywhere (so far). I've used an '_' at the end of member variables...
1
by: Just that | last post by:
Q: What does this mean ? #define IT_MEANS__ //I see this in a lot of source files I downloaded A:
96
by: Gustav Hllberg | last post by:
I tried finding a discussion around adding the possibility to have optional underscores inside numbers in Python. This is a popular option available in several "competing" scripting langauges, that...
46
by: James Harris | last post by:
Before I embark on a new long-term project I'd appreciate your advice on how to split up long names. I would like to keep the standards for command names the same as that for variable names....
3
by: Tulasi Kumar | last post by:
I am able to add my applicatoin to system tray. The problem I am facing here is.... I want add more number of instances (System tray Icons) of a single application to the system tray. At...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.