trailing underscores naming convention_

M

Metallicow

I seem to be comfortable with all the information out around the net dealing
with python naming conventions. Occasionally I have to remind myself on some
of this stuff. The PEP8 does a good job for most of it, but I am having a bit
of trouble finding some more detailed information on the trailing half of
the underscores convention.

The PEP8 says that one_ underscore is basically for helping fix
python keyword names.
OK. fair enough on that point.

But what is the standards for everything else... purely coders choice?...
....or other...
It would be nice if fellow pythoneers chimed in on the one or two trailing
underscores convention and how the use it in their code.

Ok so the situation is I have made myself a subclass of AuiManager.
In AuiManager there is a method named OnLeftDClick.
In my subclass I am not wanting to override(or hence copy the code into mine)
to get my same named method to work as normally with event.Skip().

What I am wanting to do is just add extra functionality to the
event(it doesn't matter if the event comes before or after) without
stomping on(overriding) the AuiManager method.

....so what would be proper here for my method name...?
one trailing underscore or two?
OnLeftDClick_(self, event):
or
OnLeftDClick__(self, event):

What is the norm for trailing underscores naming convention overall?
Mostly with func/meth/class naming, but attribute explanation would be nice also.
 
I

Ian Kelly

I seem to be comfortable with all the information out around the net dealing
with python naming conventions. Occasionally I have to remind myself on some
of this stuff. The PEP8 does a good job for most of it, but I am having a bit
of trouble finding some more detailed information on the trailing half of
the underscores convention.

The PEP8 says that one_ underscore is basically for helping fix
python keyword names.
OK. fair enough on that point.

But what is the standards for everything else... purely coders choice?...
...or other...
It would be nice if fellow pythoneers chimed in on the one or two trailing
underscores convention and how the use it in their code.

I'm not aware of any convention for trailing underscores other than
the one described in PEP8.
Ok so the situation is I have made myself a subclass of AuiManager.
In AuiManager there is a method named OnLeftDClick.
In my subclass I am not wanting to override(or hence copy the code into mine)
to get my same named method to work as normally with event.Skip().

What I am wanting to do is just add extra functionality to the
event(it doesn't matter if the event comes before or after) without
stomping on(overriding) the AuiManager method.

If you use a different name in the subclass then whatever code calls
the method will continue to call the base class version instead of
your subclassed version. Is there a reason you don't just use super()
to chain the call to the base method?

def OnLeftDClick(self, event):
event.Skip()
super(MyClassName, self).OnLeftDClick(event)
 
M

Metallicow

I'm not aware of any convention for trailing underscores other than
the one described in PEP8.


If you use a different name in the subclass then whatever code calls
the method will continue to call the base class version instead of
your subclassed version. Is there a reason you don't just use super()
to chain the call to the base method?

def OnLeftDClick(self, event):
event.Skip()
super(MyClassName, self).OnLeftDClick(event)

I often see in other folks code methods/funcs/classes, etc with trailing underscores in them
and and not sure if that was a convention thing or not. ...So basically
you are telling me it is coders preference/naming style?

I tried the super thing, but in this instance what you posted calls my method twice, which isn't desired.
The renaming comes from the fact that I am only targeting this
particular subclass for this event extension, otherwise
I would have made a mixin out of it. But for something as simple as a
standard event like this, unless the mixin could be used elsewhere,
outside of this subclass, then making one is basically a waste of time overall.

Anyway, my code works as expected with the _ appended on the method name.
The class method is called, and event.Skip() allows my method to be called also without missing anything. I might look at toying with the super way more(it probably just needs tweaked a bit). I don't use super in the class __init__ anyway for readability and conciseness reasons BTW, just if that super coding
style was an assumption.
I just was not sure if there was common rules for that type of stuff.
 
M

Metallicow

I guess to be more clear here is a small code snippet that shows what is happening more readably. Hence the underscores question.



class MainAuiManager(aui.AuiManager):
def __init__(self, managed_window=None, agwFlags=0)
aui.AuiManager.__init__(self, managed_window, agwFlags)

## self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDClick)
self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDClick_)
self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDClick__)
self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDClick___)
# etc...

## def OnLeftDClick(self, event):
## """This will override the aui.AuiManager.OnLeftDClick event method."""
## event.Skip()
## print('OnLeftDClick')

def OnLeftDClick_(self, event):
"""This will extend the aui.AuiManager.OnLeftDClick event method."""
event.Skip()
print('OnLeftDClick_')

def OnLeftDClick__(self, event):
"""This will extend the aui.AuiManager.OnLeftDClick event method."""
event.Skip()
print('OnLeftDClick__')

def OnLeftDClick___(self, event):
"""This will extend the aui.AuiManager.OnLeftDClick event method."""
event.Skip()
print('OnLeftDClick___')
 
P

Peter Otten

Metallicow said:
I guess to be more clear here is a small code snippet that shows what is
happening more readably. Hence the underscores question.

Working with multiple names with small differences is error-prone.
You should give a method a name that describes what it does rather than when
it's invoked:
class MainAuiManager(aui.AuiManager):
def __init__(self, managed_window=None, agwFlags=0)
aui.AuiManager.__init__(self, managed_window, agwFlags)

self.Bind(wx.EVT_LEFT_DCLICK, self.EatMagicMushroom)
self.Bind(wx.EVT_LEFT_DCLICK, self.KillBlueMonster)
self.Bind(wx.EVT_LEFT_DCLICK, self.SingDitty)
 
M

Metallicow

Working with multiple names with small differences is error-prone.
You should give a method a name that describes what it does rather than when
it's invoked:


self.Bind(wx.EVT_LEFT_DCLICK, self.EatMagicMushroom)
self.Bind(wx.EVT_LEFT_DCLICK, self.KillBlueMonster)
self.Bind(wx.EVT_LEFT_DCLICK, self.SingDitty)

While readabily counts, in this case it is pretty readable already.
Actions speak loader than words. In this case action(as with QT) is an event(in wxPython).
My actual code would only(realisticly or sanely) extend this event once per subclass if at all...
....Unless there happens to be some crazy need for more(multiple) event methods
to fire off when 1 event happens. Especially for a class bound intended for a
top level window such as a frame or dialog.

Anyway, the small snippet just shows that this can be done, but the actual
question you replied to you left unanswered. It is about the trailing underscores.

Now would KillBlueMonsterA vs KillBlueMonsterB vs KillBlueMonster_ be any more
descriptive than OnLeftDClick_ which describes the event happening itself and
the code that extends it by 1, in this case the call order.
I could see the underscore as an extension thereof.
....so maybe OnLeftDClick_Extension_Description_ might be better...
but why the trailing underscores?

Maybe others that do this normally can chime in as to
why they for example would put the underscores on the end,
if it isn't a common convention. Maybe it is like I see it, as an extextion of some
special meaning or reason therof, but isn't always obvious as most like the name
not only desciptive, but short also.
 
P

Peter Otten

Metallicow said:
While readabily counts, in this case it is pretty readable already.
Actions speak loader than words. In this case action(as with QT) is an
event(in wxPython). My actual code would only(realisticly or sanely)
extend this event once per subclass if at all...

You should reconsider Ian's advice to override the method then.

[in another post]
I tried the super thing, but in this instance what you posted calls my
method twice, which isn't desired.

Note that if you have

self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDClick)

in the base class initialiser you must not repeat it in the subclass --
perhaps that is what caused OnLeftDClick() to be invoked twice.
...Unless there happens
to be some crazy need for more(multiple) event methods to fire off when 1
event happens. Especially for a class bound intended for a top level
window such as a frame or dialog.

Anyway, the small snippet just shows that this can be done, but the actual
question you replied to you left unanswered. It is about the trailing
underscores.

My implicit answer was that you are asking for the best hammer to drive a
screw...
Now would KillBlueMonsterA vs KillBlueMonsterB vs KillBlueMonster_ be any
more descriptive than OnLeftDClick_ which describes the event happening
itself and the code that extends it by 1, in this case the call order.
I could see the underscore as an extension thereof.
...so maybe OnLeftDClick_Extension_Description_ might be better...
but why the trailing underscores?

I do not remember having seen these trailing underscores. Who is using them
where? Someone misapplying the (questionable) C++ convention to mark
members?
 
A

Albert Visser

Definitely.


Anyway, the small snippet just shows that this can be done, but the
actual
question you replied to you left unanswered. It is about the trailing
underscores.

It's not an "official" convention I think, but a (single) trailing
underscore is mainly meant to create something that is close to an
original definition without shadowing it.
If you subclass an object and bind a thusly underscored method to an event
to which the original is already bound in the superclass's __init__
method, they are both getting called on the event unless you do not call
the superclass's __init__() in your own __init__().

--
Vriendelijk groeten / Kind regards,

Albert Visser

Using Opera's mail client: http://www.opera.com/mail/
 
M

Michael Torrie

I guess to be more clear here is a small code snippet that shows what
is happening more readably. Hence the underscores question.

In a case like this I'd probably prefer to number the methods rather
than add underscores to the end of the names. My current font, for
example, connects the underscores together, so it's a bit hard from a
glance to tell if it's just two underscores or three.

I'd prefer OnLeftDClick1, OnLeftDClick2, OnLeftDClick3, for example.

And I also prefer pep8 method names as well, on_left_dclick1, etc. But
when the underlying library doesn't follow pep8, then I guess it does
not matter (self.Bind is from the library I presume).
 
S

Steven D'Aprano

In a case like this I'd probably prefer to number the methods rather
than add underscores to the end of the names. My current font, for
example, connects the underscores together, so it's a bit hard from a
glance to tell if it's just two underscores or three.

I'd prefer OnLeftDClick1, OnLeftDClick2, OnLeftDClick3, for example.

Yes, this.

I wouldn't say that underscore suffixes are *reserved* only for avoiding
name clashes with keywords and built-ins, e.g. in_ type_ etc., but I
can't think of any other reason why you would want to end an identifier
with an underscore. As for multiple underscores like in__ in___ in____
that's just too hideous for words.

And I also prefer pep8 method names as well, on_left_dclick1, etc. But
when the underlying library doesn't follow pep8, then I guess it does
not matter (self.Bind is from the library I presume).

And again, this.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top