# 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

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

## Multimedia

* YouTube: Python Classes and Objects
* YouTube: Python init
* YouTube: Python Class vs. Instance Variables
* YouTube: Python Inheritance
* YouTube: Python Classes and Objects Playlist

## Examples
### 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:

### 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:

## Activities
### Tutorials

* Complete one or more of the following tutorials:
** LearnPython
*** Classes and Objects
** TutorialsPoint
*** Object Oriented
** Codecademy
*** Introduction to Classes
** Wikiversity
*** Python/Classes
** Wikibooks
*** Wikibooks: A Beginner's Python Tutorial/Classes

### Practice

* 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.
* 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.
* 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.
* 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.

## Lesson Summary
### 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:
class ClassName: <statements>
* Classes are instantiated using the syntax:
variable = ClassName
* Class attributes (variables) are defined using the syntax:
class ClassName: attribute = value
* Class methods (functions) are defined using the syntax: class ClassName: def function(self): <statements>
* Class attributes may be initialized during instantiation using the __init__ method. class ClassName: def __init__(self, attribute[, attribute, ...]): <statements>
* Subclasses are defined using the syntax: class DerivedClassName(BaseClassName): <statements>

## 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).

## Review Questions

* Object-oriented programming (OOP) is _____. 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 _____. 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 _____. 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 _____. Objects are instances of classes.
* Class variables belong to _____. Class variables belong to the class as a whole; there is only one copy of each one.
* Instance variables or attributes belong to _____. Instance variables or attributes belong to individual objects; every object has its own copy of each one.
* Class methods belong to _____. 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 _____. 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 _____. 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 _____. 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 _____. Objects are created by calling a special type of method in the class known as a constructor.
* A program may create many _____ 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. 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 _____. 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 _____. Subclasses can override the methods defined by superclasses.
* Classes are defined using the syntax: Classes are defined using the syntax: class ClassName: <statements>
* Classes are instantiated using the syntax: {{{2}}}
* Class attributes (variables) are defined using the syntax: {{{2}}}
* Class methods (functions) are defined using the syntax: Class methods (functions) are defined using the syntax: class ClassName: def function(self): <statements>
* Class attributes may be initialized during instantiation using the ____ method. Class attributes may be initialized during instantiation using the __init__ method. class ClassName: def __init__(self, attribute[, attribute, ...]): <statements>
* Subclasses are defined using the syntax: Subclasses are defined using the syntax: class DerivedClassName(BaseClassName): <statements>

## Assessments

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

## See Also

* Python.org: Classes
* PythonBasics: classes and oop tutorial
* SoloLearn: Python
* Open Book Project: Python 3 - Classes and Objects
* Stéphane Ducasse - Free Online Books: Designing Object Systems