PEP8 Style for class instances?

A forum for general discussion of the Python programming language.

PEP8 Style for class instances?

Postby Perry » Fri Jul 26, 2013 2:07 pm

I was hoping someone could tell me what the PEP8 style is for class instances. The style guide just says CamelCase for classes, but I usually see people using mixedCase for instances. I don't mean instance variables either, but the instance itself.

Code: Select all
someClassInstance = SomeClass()

Code: Select all
SomeClassInstance = SomeClass()

Code: Select all
some_class_instance = SomeClass()

Personally I like using mixed for the instance itself. I think it marks it clearly as an instance. Just wondering.
Posts: 2
Joined: Fri Jul 26, 2013 2:01 pm

Re: PEP8 Style for class instances?

Postby stranac » Fri Jul 26, 2013 2:45 pm

PEP8 recommends lower_case_with_underscores.

Also, I pretty much never see mixedCase used for instance names, at least not in python.
I do know a few people who use it for functions and/or classes.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

Re: PEP8 Style for class instances?

Postby Perry » Fri Jul 26, 2013 2:54 pm

I'm seeing it in the wxPython tutorials like this.
Code: Select all
  15         filemenu= wx.Menu()
  16         menuOpen = filemenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit")
  17         menuAbout= filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
  18         menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
  20         # Creating the menubar.
  21         menuBar = wx.MenuBar()
  22         menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
  23         self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.

I've seen it in some other tutorials I think. Thanks for clearing that up though. I'd been wondering and couldn't find anything specific.
Posts: 2
Joined: Fri Jul 26, 2013 2:01 pm

Re: PEP8 Style for class instances?

Postby Mekire » Fri Jul 26, 2013 4:02 pm

stranac wrote:PEP8 recommends lower_case_with_underscores.

Could you tell me where in the document it says this. I have read it many times and can't find reference to instance naming conventions.

This section here:
isn't referring to the naming of the instance itself, but rather to the self.whatever attribute naming within the class.

I used CamelCase for classes and instances alike for quite some time. Lately I have been using lower_case_with_underscores for the instances. I really would be interested in a definitive answer on this.

mixedCase is a general no-no in modern python code. You occasionally see it in older stuff (including the well known "rat book") and you see it in the code of those who started programming with another language before python.

PEP8 wrote:mixedCase is allowed only in contexts where that's already the prevailing style (e.g., to retain backwards compatibility.

New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Posts: 1710
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Re: PEP8 Style for class instances?

Postby Yoriz » Fri Jul 26, 2013 5:00 pm

Code: Select all
class ClassName(object):
    '''^ is a class name'''
This is clear that pep8 talks about ClassNames in this part
Class Names
Almost without exception, class names use the CapWords convention.
Classes for internal use have a leading underscore in addition.

Code: Select all
instance = ClassName()
'''^ is a module namespace variable name pointing to a instance object
   of ClassName'''
This is not so clearcut but its not a class name as it isn't preceded by 'class', it is a variable name that is pointing to an instance object.
This variable name is global to the modules namespace, my guess(and its only a guess) is that when they state 'Global Variable Names' in pep8,
it means variables that are global to the module namespace.
So it would come under this part.
Global Variable Names

(Let's hope that these variables are meant for use inside one module only.) The conventions are about the same as those for functions.

Modules that are designed for use via from M import * should use the __all__ mechanism to prevent exporting globals, or use the older convention of prefixing such globals with an underscore (which you might want to do to indicate these globals are "module non-public").

It states they follow the same style as functions.
Function Names

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

mixedCase is allowed only in contexts where that's already the prevailing style (e.g., to retain backwards compatibility.

Thats a long winded way to conclude that
PEP8 recommends lower_case_with_underscores.

Search through the python docs and see if they use CamelCase for the naming of any instance object variable names.

I found this where they create a class and then use a lowercase variable name for it.
Code: Select all
class Employee:

john = Employee() # Create an empty employee record

Due to the reasons discussed here we will be moving to on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Return to General Discussions

Who is online

Users browsing this forum: Bing [Bot] and 11 guests