trailing underscores naming convention_

Metallicow metaliobovinus at gmail.com
Fri May 9 01:03:04 EDT 2014


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.



More information about the Python-list mailing list