Python Programming Style Guide

 

Storage Classes

ReadabilityPython Style GuideSyntax Restrictions

Storage classes are used to store a structured or multi-component data record. By using a storage class, the individual data components can be referenced by name instead of using a tuple or list structure and referencing the components by index. A data field name provides a more meaningful indicator of the type of data and its purpose as compared to a simple integer index value.

Naming

The name of a storage class should follow the rules for naming classes:

  • The name should be descriptive of the data actually stored in instances of the storage class.
  StudentRecord   ListNode   GridCell   MatrixElement   MapElement
  • The name should consist of more than one word and typically ends with one of the following nouns:
Record    Element   Cell    Node    Item
  • Do not name a storage class Storage or Record.

Storage classes must be commented with a description and an indication of their visibility.

Constructor

A storage class should only include a constructor that either takes no arguments

class StudentRecord:          
  def __init__( self ):      # Do this.
    self.idNum = 0
    self.firstName = None
    self.lastName = None
    self.gpa = 0.0

or arguments comprised of all or a subset of the data fields defined by the class

class StudentRecord:          
  def __init__( self, idNum, firstName, lastName ):      # Or do this.
    self.idNum = idNum
    self.firstName = firstName
    self.lastName = lastName
    self.gpa = 0.0

No processing or executable statements other than assignments should be performed within the constructor of a storage class. For example, the following should not be done

class StudentRecord:          
  def __init__( self, line ):      # Don't do this.
    lineParts = line.split()
    self.idNum = int(lineParts[0])
    self.firstName = lineParts[1]
    self.lastName = lineParts[2]
    self.gpa = 0.0

Data Attributes

The data fields or attributes of a storage class should:

  • have meaningful names as related to the data they store.
  • be public since they are directly accessed by the code that uses them.



ReadabilityPython Style GuideSyntax Restrictions
Print -- Recent Changes
Page last modified on September 24, 2012, at 12:46 PM