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

or
Code: Select all
SomeClassInstance = SomeClass()

or
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.
Perry
 
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
stranac
 
Posts: 1216
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")
  19
  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.
Perry
 
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:
http://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables
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. threading.py), to retain backwards compatibility.


-Mek
User avatar
Mekire
 
Posts: 1025
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

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. threading.py), 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:
    pass

john = Employee() # Create an empty employee record

here http://docs.python.org/2/tutorial/classes.html#odds-and-ends
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1049
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests