Class Variables and Class Methods

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Objectives:

  • Learn about class variables and class methods

Files Modified: stock.py, reader.py


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") python/AdvancedTopicsGroup -.-> python/iterators("`Iterators`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/with_statement -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/variables_data_types -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/numeric_types -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/strings -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/type_conversion -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/for_loops -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/list_comprehensions -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/lists -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/tuples -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/dictionaries -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/function_definition -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/importing_modules -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/using_packages -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/standard_libraries -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/classes_objects -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/constructor -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/polymorphism -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/encapsulation -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/file_opening_closing -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/iterators -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/data_collections -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/python_shell -.-> lab-132493{{"`Class Variables and Class Methods`"}} python/build_in_functions -.-> lab-132493{{"`Class Variables and Class Methods`"}} end

Previous Lab

Instances of the Stock class defined in the previous lab are normally created as follows:

>>> s = Stock('GOOG', 100, 490.1)
>>>

However, the read_portfolio() function also creates instances from rows of data read from files. For example, code such as the following is used:

>>> import csv
>>> f = open('portfolio.csv')
>>> rows = csv.reader(f)
>>> headers = next(rows)
>>> row = next(rows)
>>> row
['AA', '100', '32.20']
>>> s = Stock(row[0], int(row[1]), float(row[2]))
>>>

Alternate constructors

Perhaps the creation of a Stock from a row of raw data is better handled by an alternate constructor. Modify the Stock class so that it has a types class variable and from_row() class method like this:

class Stock:
    types = (str, int, float)
    def __init__(self, name, shares, price):
        self.name = name
        self.shares = shares
        self.price = price

    @classmethod
    def from_row(cls, row):
        values = [func(val) for func, val in zip(cls.types, row)]
        return cls(*values)
    ...

Here's how to test it:

>>> row = ['AA', '100', '32.20']
>>> s = Stock.from_row(row)
>>> s.name
'AA'
>>> s.shares
100
>>> s.price
32.2
>>> s.cost()
3220.0000000000005
>>>

Carefully observe how the string values in the row have been converted to a proper type.

Class variables and inheritance

Class variables such as types are sometimes used as a customization mechanism when inheritance is used. For example, in the Stock class, the types can be easily changed in a subclass. Try this example which changes the price attribute to a Decimal instance (which is often better suited to financial calculations):

>>> from decimal import Decimal
>>> class DStock(Stock):
        types = (str, int, Decimal)

>>> row = ['AA', '100', '32.20']
>>> s = DStock.from_row(row)
>>> s.price
Decimal('32.20')
>>> s.cost()
Decimal('3220.0')
>>>

Design Discussion

The problem being addressed in this lab concerns the conversion of data read from a file. Would it make sense to perform these conversions in the __init__() method of the Stock class instead? For example:

class Stock:
    def __init__(self, name, shares, price):
        self.name = str(name)
        self.shares = int(shares)
        self.price = float(price)

By doing this, you would convert a row of data as follows:

>>> row = ['AA', '100', '32.20']
>>> s = Stock(*row)
>>> s.name
'AA'
>>> s.shares
100
>>> s.price
32.2
>>>

Is this good or bad? What are your thoughts? On the whole, I think it's a questionable design since it mixes two different things together--the creation of an instance and the conversion of data. Plus, the implicit conversions in __init__() limit flexibility and might introduce weird bugs if a user isn't paying careful attention.

Reading Objects

Change the read_portfolio() function to use the new Stock.from_row() method that you wrote.

Point of thought: Which version of code do you like better? The version of read_portfolio() that performed the type conversions or the one that performs conversions in the Stock.from_row() method? Think about data abstraction.

Can you modify read_portfolio() to create objects using a class other than Stock? For example, can the user select the class that they want?

Generalizing

A useful feature of class-methods is that you can use them to put a highly uniform instance creation interface on a wide variety of classes and write general purpose utility functions that use them.

Earlier, you created a file reader.py that had some functions for reading CSV data. Add the following read_csv_as_instances() function to the file which accepts a class as input and uses the class from_row() method to create a list of instances:

## reader.py
...

def read_csv_as_instances(filename, cls):
    '''
    Read a CSV file into a list of instances
    '''
    records = []
    with open(filename) as f:
        rows = csv.reader(f)
        headers = next(rows)
        for row in rows:
            records.append(cls.from_row(row))
    return records

Get rid of the read_portfolio() function--you don't need that anymore. If you want to read a list of Stock objects, do this:

>>> ## Read a portfolio of Stock instances
>>> from reader import read_csv_as_instances
>>> from stock import Stock
>>> portfolio = read_csv_as_instances('portfolio.csv', Stock)
>>> portfolio
[<__main__.Stock object at 0x100674748>,
<__main__.Stock object at 0x1006746d8>,
<__main__.Stock object at 0x1006747b8>,
<__main__.Stock object at 0x100674828>,
<__main__.Stock object at 0x100674898>,
<__main__.Stock object at 0x100674908>,
<__main__.Stock object at 0x100674978>]
>>>

Here is another example of how you might use read_csv_as_instances() with a completely different class:

>>> class Row:
         def __init__(self, route, date, daytype, numrides):
             self.route = route
             self.date = date
             self.daytype = daytype
             self.numrides = numrides
         @classmethod
         def from_row(cls, row):
             return cls(row[0], row[1], row[2], int(row[3]))

>>> rides = read_csv_as_instances('ctabus.csv', Row)
>>> len(rides)
577563
>>>

Discussion

This lab illustrates the two most common uses of class variables and class methods. Class variables are often used to hold a global parameter (e.g., a configuration setting) that is shared across all instances. Sometimes subclasses will inherit from the base class and override the setting to change behavior.

Class methods are most commonly used to implement alternate constructors as shown. A common way to spot such class methods is to look for the word "from" in the name. For example, here is an example on built-in dictionaries:

>>> d = dict.fromkeys(['a','b','c'], 0)     ## class method
>>> d
{'a': 0, 'c': 0, 'b': 0}
>>>

Summary

Congratulations! You have completed the Class Variables and Class Methods lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like