Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #71150 > unrolled thread

trailing underscores naming convention_

Started byMetallicow <metaliobovinus@gmail.com>
First post2014-05-08 20:28 -0700
Last post2014-05-10 00:00 +0000
Articles 10 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  trailing underscores naming convention_ Metallicow <metaliobovinus@gmail.com> - 2014-05-08 20:28 -0700
    Re: trailing underscores naming convention_ Ian Kelly <ian.g.kelly@gmail.com> - 2014-05-08 22:24 -0600
      Re: trailing underscores naming convention_ Metallicow <metaliobovinus@gmail.com> - 2014-05-08 22:03 -0700
    Re: trailing underscores naming convention_ Metallicow <metaliobovinus@gmail.com> - 2014-05-08 22:49 -0700
      Re: trailing underscores naming convention_ Peter Otten <__peter__@web.de> - 2014-05-09 11:10 +0200
        Re: trailing underscores naming convention_ Metallicow <metaliobovinus@gmail.com> - 2014-05-09 03:22 -0700
          Re: trailing underscores naming convention_ Peter Otten <__peter__@web.de> - 2014-05-09 13:49 +0200
          Re: trailing underscores naming convention_ "Albert Visser" <albert.visser@gmail.com> - 2014-05-09 16:24 +0200
      Re: trailing underscores naming convention_ Michael Torrie <torriem@gmail.com> - 2014-05-09 10:35 -0600
        Re: trailing underscores naming convention_ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-05-10 00:00 +0000

#71150 — trailing underscores naming convention_

FromMetallicow <metaliobovinus@gmail.com>
Date2014-05-08 20:28 -0700
Subjecttrailing underscores naming convention_
Message-ID<57d21b4f-10db-4fbf-82f4-d33250b14456@googlegroups.com>
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.

[toc] | [next] | [standalone]


#71151

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-05-08 22:24 -0600
Message-ID<mailman.9812.1399609490.18130.python-list@python.org>
In reply to#71150
On Thu, May 8, 2014 at 9:28 PM, Metallicow <metaliobovinus@gmail.com> wrote:
> 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)

[toc] | [prev] | [next] | [standalone]


#71152

FromMetallicow <metaliobovinus@gmail.com>
Date2014-05-08 22:03 -0700
Message-ID<f9877ba4-4b5e-4563-bbf1-7318ca44d1e4@googlegroups.com>
In reply to#71151
On Thursday, May 8, 2014 10:24:00 PM UTC-6, Ian wrote:
> On Thu, May 8, 2014 at 9:28 PM, Metallicow wrote:
> > 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)

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.

[toc] | [prev] | [next] | [standalone]


#71154

FromMetallicow <metaliobovinus@gmail.com>
Date2014-05-08 22:49 -0700
Message-ID<57b0d9ec-b8a4-4021-b25f-422a9fe11529@googlegroups.com>
In reply to#71150
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___')

[toc] | [prev] | [next] | [standalone]


#71162

FromPeter Otten <__peter__@web.de>
Date2014-05-09 11:10 +0200
Message-ID<mailman.9818.1399627734.18130.python-list@python.org>
In reply to#71154
Metallicow wrote:

> 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)

[toc] | [prev] | [next] | [standalone]


#71164

FromMetallicow <metaliobovinus@gmail.com>
Date2014-05-09 03:22 -0700
Message-ID<8b8daad1-0e84-4c2f-a91c-696487df2819@googlegroups.com>
In reply to#71162
On Friday, May 9, 2014 3:10:26 AM UTC-6, Peter Otten wrote:
> Metallicow wrote:
> 
> > 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)

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.

[toc] | [prev] | [next] | [standalone]


#71166

FromPeter Otten <__peter__@web.de>
Date2014-05-09 13:49 +0200
Message-ID<mailman.9819.1399636167.18130.python-list@python.org>
In reply to#71164
Metallicow wrote:

> On Friday, May 9, 2014 3:10:26 AM UTC-6, Peter Otten wrote:
>> Metallicow wrote:
>> 
>> > 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)
> 
> 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?

> 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.

[toc] | [prev] | [next] | [standalone]


#71173

From"Albert Visser" <albert.visser@gmail.com>
Date2014-05-09 16:24 +0200
Message-ID<mailman.9822.1399645466.18130.python-list@python.org>
In reply to#71164
On Fri, 09 May 2014 12:22:56 +0200, Metallicow <metaliobovinus@gmail.com>  
wrote:

> On Friday, May 9, 2014 3:10:26 AM UTC-6, Peter Otten wrote:
>> Metallicow wrote:
>>
>> > 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.

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/

[toc] | [prev] | [next] | [standalone]


#71180

FromMichael Torrie <torriem@gmail.com>
Date2014-05-09 10:35 -0600
Message-ID<mailman.9824.1399653332.18130.python-list@python.org>
In reply to#71154
On 05/08/2014 11:49 PM, Metallicow wrote:
> 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).


> 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___')
> 

[toc] | [prev] | [next] | [standalone]


#71202

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-05-10 00:00 +0000
Message-ID<536d6bff$0$29980$c3e8da3$5496439d@news.astraweb.com>
In reply to#71180
On Fri, 09 May 2014 10:35:09 -0600, Michael Torrie wrote:

> On 05/08/2014 11:49 PM, Metallicow wrote:
>> 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.

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.


-- 
Steven D'Aprano
http://import-that.dreamwidth.org/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web