Python Programming/Classes

This lesson introduces Python classes.

Objectives and Skills
Objectives and skills for this lesson include:
 * Object Oriented Programming
 * The self
 * Classes
 * Object Methods and the __init__ method
 * Class And Object Variables
 * Inheritance

Readings

 * 1)  Object-oriented programming
 * 2) Sthurlow: Python Classes
 * 3) Jess Hamrick: An Introduction to Classes and Inheritance (in Python)

Multimedia

 * 1) YouTube: Python Classes and Objects
 * 2) YouTube: Python init
 * 3) YouTube: Python Class vs. Instance Variables
 * 4) YouTube: Python Inheritance
 * 5) YouTube: Python Classes and Objects Playlist

Class Definition
Classes are defined using the syntax:

Class Instantiation
Classes are instantiated using the syntax:

Class Attributes
Class attributes (variables) are defined using the syntax:

Class Methods
Class methods (functions) are defined using the syntax:

Class Initialization
Class attributes may be initialized during instantiation using the __init__ method.

Class Example
The following example creates a class named Shape with an attribute named sides and a method named perimeter. Three instances of the class are created, one each for a triangle, rectangle, and square. The attribute value and method result are printed for each shape.

Output: triangle sides: [3, 4, 5] triangle perimeter: 12 rectangle sides: [4, 2, 4, 2] rectangle perimeter: 12 square sides: [2, 2, 2, 2] square perimeter: 8

Inheritance
Subclasses are defined using the syntax:

Inheritance Example
The following example creates a class named Shape with an attribute named sides and a method named perimeter. Three subclasses are created named Triangle, Rectangle, and Square. An instance of each subclass is created, and the attribute value and method result are printed for each shape.

Output: triangle sides: [3, 4, 5] triangle perimeter: 12 rectangle sides: [4, 2, 4, 2] rectangle perimeter: 12 square sides: [2, 2, 2, 2] square perimeter: 8

Tutorials

 * 1) Complete one or more of the following tutorials:
 * 2) * LearnPython
 * 3) ** Classes and Objects
 * 4) * TutorialsPoint
 * 5) ** Object Oriented
 * 6) * Codecademy
 * 7) ** Introduction to Classes
 * 8) * Wikiversity
 * 9) ** Python/Classes
 * 10) * Wikibooks
 * 11) ** A Beginner's Python Tutorial/Classes

Practice

 * 1) Create a Python program that asks the user how old they are in years. Then ask the user if they would like to know how old they are in months, days, hours, or seconds.  Use a condition statement to determine their selection, and display their approximate age in the selected timeframe. Perform all calculations using an AgeConverter class that accepts the age in years during initialization and has separate methods that calculate and return the age in months, days, hours, and seconds. Include data validation in the class and error handling in the main program.
 * 2) Review MathsIsFun: Conversion of Temperature. Create a Python program that asks the user if they would like to convert Fahrenheit to Celsius or Celsius to Fahrenheit.  Use a condition statement to determine their selection and then gather the appropriate input. Perform all calculations using a TemperatureConverter class that accepts the temperature during initialization and has separate methods that calculate and return the temperature in Celsius and Fahrenheit. Include data validation in the class and error handling in the main program.
 * 3) Review MathsIsFun: Area of Plane Shapes.  Create a Python program that asks the user what shape they would like to calculate the area for. Use a condition statement to determine their selection and then gather the appropriate input. Perform all area calculations using a Shape main class and subclasses for each selected shape type. Include data validation in the class and error handling in the main program.
 * 4) Create a Python program that contains a dictionary of names and phone numbers. Use a class to contain and maintain the dictionary, and use methods to add, update, remove, and search for names and numbers. Include data validation in the class and error handling in the main program.

Class Concepts

 * Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.
 * A feature of objects is that an object's procedures can access and often modify the data fields of the object with which they are associated through a notion of "this" or "self".
 * Classes define the data format and available procedures for a given type of object and may also contain data and procedures (known as class attributes and methods).
 * Objects are instances of classes.
 * Class variables belong to the class as a whole; there is only one copy of each one.
 * Instance variables or attributes belong to individual objects; every object has its own copy of each one.
 * Class methods belong to the class as a whole and have access only to class variables and inputs from the procedure call.
 * Instance methods belong to individual objects, and have access to instance variables for the specific object they are called on, inputs, and class variables.
 * Classes have a unique namespace so code in one class will not be accidentally confused with the same procedure or variable name in another class.
 * Classes provide a layer of abstraction which can be used to separate internal from external code. External code can use an object by calling a specific instance method with a certain set of input parameters, read an instance variable, or write to an instance variable.
 * Objects are created by calling a special type of method in the class known as a constructor.
 * A program may create many instances of the same class as it runs, which operate independently. This is an easy way for the same procedures to be used on different sets of data.
 * Inheritance allows classes to be arranged in a hierarchy that represents "is-a-type-of" relationships. All data and methods available to the parent class also appear in the child class with the same names.
 * Subclasses can override the methods defined by superclasses.

Python Classes

 * Classes are defined using the syntax:
 * Classes are instantiated using the syntax:
 * Class attributes (variables) are defined using the syntax:
 * Class methods (functions) are defined using the syntax:
 * Class attributes may be initialized during instantiation using the __init__ method.
 * Subclasses are defined using the syntax:

Key Terms

 * attribute
 * Data encapsulated within a class or object.


 * class
 * An extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).


 * constructor
 * A special type of procedure called to create an object which prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.


 * inheritance
 * When an object or class is based on another object or class using the same implementation to maintain the same behavior.


 * instance
 * A concrete occurrence of an object.


 * instantiate
 * To create an occurrence of an object, typically by calling its constructor.


 * method
 * A procedure associated with an object.


 * object
 * A particular instance of a class.


 * subclass
 * A modular, derivative class that inherits one or more language entities from one or more other classes (called superclasses, base classes, or parent classes).

Assessments

 * Flashcards: Quizlet: Python Classes
 * Quiz: Quizlet: Python Classes