T-Th 9:05
or
T-Th 11:15
in Olin 155

CS 1110: Introduction to Computing Using Python

Fall 2012

Object-oriented Concepts and Definitions

Learning a new language is always jargon heavy. Below, we present several definitions and object-oriented concepts that you may see throughout the course. When possible, we provide references to the course text.

[a] [b] [c] [d] [e] [f] [g] [h] [i] [j] [k] [l] [m] [n] [o] [p] [q] [r] [s] [t] [u] [v] [w] [x] [y] [z]

active namespace
The namespace of the current module, or the interactive-shell, whichever is currently being edited. global variables, functions, and classes in the active namespace do not need to be preceded by the name of the namespace when used.
application
A Python application is a program that contains a segment of code like this:

    if __name__ == "__main__":

Applications can be run outside of the interactive shell. To run an application, type python <application name>  at the OS command shell.
argument
An expression that occurs within the parentheses of a method call. The following call has two arguments: x+y and y+w:

    min(x+y, y+w)

As a general rule, the number of arguments should equal the number of parameters that are called, except when using default arguments.
assert
A Python keyword that is followed by a boolean expression and (optionally) a string. If the boolean expression is False, Python raises an exception with the provided string as a message. Assert statements are distinct from assertions, though they may be used to enforce assertions.
assertion
A true-false statement about the variables in a program that is placed as a comment at a particular place in the program to indicate that the true-false statement is true at that place. See also precondition and postcondition.
assignment statement
A statement of the following form:

    <variable> = <value>

If the variable does not yet exist, the statement creates the variable and stores the given value inside it. If the variable does exist, then it replaces the old value with the one provided.

backquote:   `
The symbol on the keyboard underneath the tilde key. Putting an expression inside of backquotes evaluates the expression and then converts the expression to a string. In the case of objects, backquotes use the object representation to determine the string.
boolean
A primitive type whose values are True and False.
bottom-up rule
The rule that indicates that, when looking for a variable or method in an object, start from the bottom and look upward. This rule ensures that an overriding method will be called (if present) rather than an inherited one.

cast
A cast is an operation that converts a value from one type to another. For example, the cast float(5+6) converts the value of the expression, which is 11, to float format. A widening cast may be done implicitly; a narrowing cast must be done explicitly, because it may lose information or may be illegal. See narrower type).
class
A class definition defines the format of objects (or instances or folders) of the class —it defines the methods, fields, and properties that go in each object of the class.
class expression
A class expression has the form <class-name>(<arguments>). It is evaluated in three steps:
  1. Create an instance of class <class-name>;
  2. Execute the constructor __init__(<arguments>);
  3. Yield as value the name of the newly created instance
class invariant
The collection of meanings and constraints on the fields and properties of a class that describe the valid states of all instances of the class before and after each method is called. The class invariant should appear as comments on the declarations of the fields. A constructor should truthify the class invariant. Each method body and property can assume that the class invariant is true and should terminate with the class invariant true, for all objects.
class method
A method that is declared with modifier @classmethod. There is only one copy of the method, and it resides in the class itself (in the class file drawer). All class methods have cls as their first parameter.
class type
A type that is the name of a class. Its values are the names of objects of that class.
command shell
An OS-specific application that responds to text commands. On Windows it is called the Command Prompt. On OS X, it is called the Terminal.
component
A component of a class is a variable or method that is declared in the class or is inherited from the superclass.
conditional expression
An expression of the following form:

    <boolean-expression> if <expression-1> else <expression-2>

To evaluate it, evaluate the <boolean-expression>; if it is true, use the value of <expression-1> as the value of the conditional expression; otherwise, use the value of <expression-2> as the value of the conditional expression).
constructor
A method that is declared with the basic form:

    __init__(<parameters>):

The purpose of a constructor is to initialize some or all of the fields of a new instance of class <class-name> when the instance is created. See also constructor call.
constructor call
A call of a constructor. There are two ways to call a constructor —the second way may appear only within a constructor body:

1. In a class expression.
2. As a call of a constructor of the superclass:
    super(<class-name>,self).__init__(<arguments>);
constructor, default
If (and only if) a class definition does not contain a constructor, it uses the one that it inherits from its subclass, which may be the class object.

default argument
An argument that is provided automatically if it is omitted in the function call. Default arguments are specified by writing the associated parameter in the form of an assignment statement. For example, in the following function header

    def foo(x,y=2):

The function call foo(1) is legal. In that case, y is given the default argument 2.
delete statement
A statement which eliminates a variable. It has the following form:

    del <variable>

Attempting to reference a variable after it is deleted will cause Python to
raise an exception.
deleter
An invisible method that is associated with a property. It is defined with the following syntax:

    @<property>.deleter
    def <property>(self):


An delete statement to a property is converted to a method call on the deleter.
deprecate
To deprecate means to lessen in value. Some functions and syntax has been depricated in the various versions of Python. Code that uses depricated syntax is not guaranteed to work in later versions of Python. Indeed, this is one of the reasons why we use Python 2.7.x, and not Python 3.
dictionary
A Python sequence type that associates keys with values. Instead of using position indices, dictionary elements are referenced by the corresponding key. For example, in the dictionary

    d = { "alpha":1, "beta":3 }

the value d["alpha"] is 1, while the value d["beta"] is 3.
docstring
A string literal that begins and ends with three quotation marks. Document strings are used to write function specs and are displayed by the help() command.

escape character:   \
Character \, the escape character, is used to write characters that cannot appear directly in a String. Here are a few (not exhaustive) examples:

New-line Character \n
Backslash Character \\
Double-Quote Character \"
Single-Quote Character \'

exception
A value that represents an error that has occurred in a program. When an exception is raised, Python stops the flow of execution and reports the exception to the user.

field
A variable belonging to a class. To reference a field, it must be preceded by a variable containing the object name. For example, to reference the field x inside of an object stored in the variable a, we use a.x
file drawer
In order to provide an easy-to-understand introduction to classes and objects, we view a class as a file drawer, which holds (1) the objects (i.e. manila folders) and (2) the class methods. The Python class definition defines the format of the content manila folders (see class).
float
A primitive type whose values are scientific numbers, e.g. 3.46E–4.
folder, manila
Our analogy for an object or instance.
frame for a function call
When a function is called, a frame is created for the call. This frame contains the following:
  1. the name of the method
  2. a program counter, which indicates the statement to execute next
  3. the parameters of the method
  4. the local variables of the method
fruitful function
A function that terminates by executing a return statement, giving an expression whose value is to be returned. Fruitful functions (possibly) return a value other than None.
function
A set of instructions to be carried out. A function is analogous to a recipe in a cookbook. We often separate functions into fruitful functions and procedures.
function body
A method consists of a method header followed by the method body, which is indented by one space under the header. When the method is called, its body is executed.
function call
An expression that ends up executing the method body. Its form is as follows:

    <function-name>(<arguments>)

It is important to understand how a method call is executed. Its execution is performed in five steps:
  1. Draw a frame for the call
  2. Assign argument values to the parameters
  3. Execute the function body
  4. Erase the frame for the call
  5. Return the function value)
If the function is a fruitful function, it returns the value of the appropriate return statement. If it is a procedure, it returns the value None.
function header
(See header, of a function).
function name
The name of the method, defined in the function header.
function specification
The specification of a function defines what the function does. It is used to understand how to write calls on the function. It must be clear, precise, and thorough, and it should mention all parameters, saying what they are used for. It is a contract between the programmer who wrote the function and users of the function. It may be given in terms of a precondition and postcondition.
Function specifications are typically written as a docstring.

getter
An invisible method that is associated with a property. It is defined with the following syntax:

    @property
    def <property>(self):


Any time this property is used in an expression, the property is converted to a method call on the getter.
global variable
A variable that is defined outside of an function or class. Global variables are typically part of a module, and store import values for reference.

header, of a function
The part of a declaration of a function that gives the function name, and the parameter declarations delimited by parentheses. It is followed, indented underneath, by the function body.
header, of a method
The function header for a method. All method headers are indented within the class definition. In addition, the first parameter of a method header should always be the keyword self.

import statement
The import statement has one of the following forms:

    import <module>        # preserve module namespace
    from <moduleimport * # import into active namespace

It is used to access the global variables, functions, and classes within a module. These values retain their original namespace unless imported with the from keyword; in that case they become part of the active namespace and can be used normally.
inheritance
A subclass inherits all the fields and instance methods available in its superclass. This means that each instance of the subclass contains not only the instance fields and methods defined in it but also the instance fields and methods defined in an inherited by the superclass.
instance
An instance of a class is an object; in our analogy instance, object, and manila folder are synonyms.
instance method
A method that is defined without the modifer @classmethod. A copy of the method resides in each instance of the class. All instance methods have self as their first parameter.
instantiate
To create an instance of. Evaluation of a class expression C(...) instantiates class C, producing a new instance of it.
int
A primitive type whose values are integers.
interactive shell
A program that allows the user to type in Python expressions and statements one at a time and evalautes them.
invariant
See class invariant.

literal
A Python denotation of a value. Literals are used to initialize any of the primitive types, or initialize Strings. Here are a few examples of literals:

Type int 354
Type float 3.56
Type boolean True
Type String "Hello World!"
Type String 'Hello World!'
local variable
A variable that is declared within the body of a function or method.

manila folder
Our analogy for an object or instance.
method
A function that is contained within a class definition. Methods are able to access the fields of the class, in addition to the parameters and local variables.
method body
The function body of a method. When the method is called, its body is executed.
method call
A function call for a method.
method header
(See header, of a method).
method name
The name of the method, defined in the method header.
method specification
The specification of a method defines what the method does. It is identical to a function specification.
module
A file containing global variables, functions, classes and other python code. The file containing the module must be the same name as the module and must end in ".py" A module is used by either importing it or running it as an application.

namespace
Any call to a global variable, function, or class must be preceded by its namespace, unless it is in the active namespace. The contents of any module are in the namespace whose name is that module, unless imported with the from keyword.
For example, if the module foo has function bar, then the proper function call is foo.bar() and not bar().
narrower type
A type from which Python will perform a cast automatically; that is Python will automatically cast from a narrower type to a wider type. However, casts from a wider type to a narrower type must be explicitly asked for in a Java program. Below, each line gives a series of types, beginning with the narrowest type and proceeding to the widest type:

   boolean --> int --> long --> float --> complex
None
A value that means "the absence of an value". If Python attepts a reference like None.b or a method call like None.m(...), it raises an exception.

object
An instance of a class; in our analogy we often refer to these as a manila folder. Each object of a class contains the fields and methods defined in and inherited by the class (See class).
Object, class
The superest class of them all: any class that does not explicitly extend another class automatically extends class Object.
object type
(See class type).
override
A method that is inherited from the superclass can be overridden by redeclaring it in the subclass. The bottom-up rule ensures that the overriding method will be called, rather than the inherited one.

parameter
A variable that is declared within the parentheses of the header of a function or method.
postcondition
An assertion that indicates what is to be true at the end of execution of a function body or, more generally, of any sequence of statements.
precedence of operators
Standard mathematics gives precedence to multiplication * over addition +, so that the expression 2 + 3 * 5 is evaluated as if it were parenthesized like this: 2 + (3 * 5). The precedences used in Python for all operators are given on this page.
precondition
An assertion that indicates what is to be true at the beginning of execution of a function body or, more generally, of any sequence of statements. The following example illustrates our convention for preconditions of a method. The specification of function foo states that in any call of foo, its argument must be at least 0. If the argument is less than 0, it is the user's fault, and the specification says nothing about what the function will do in that case. The function does not have to check for that condition.

    def foo(x):
        """Precondition: x >= 0 """

primitive type
A type that is fully integrated into Python. In other words, one that is not defined by a class definition. The primitive types of Python are: int, long, float, complex and boolean.
procedure
A function that has no explicit return statements that yield a value. A function call on a procedure always evaluates to None.
promoting a value
Same as casting it to a wider type, this is done automatically in Python when necessary. See narrower type.
property
A feature of a class that looks and behaves like a field. However, attempts to use it as a variable are substituted with method calls, according to its setter, getter, and deleter methods. The purpose of properties is that it is easier to enforce class invariants with them than it is with fields.

raise exception
The process of stopping the execution of a Python program when an error occurs. Details of the error are stored in an exception.
representation, object
A string that represents an object. It is retrieved via use of the backquotes. It is implemented via the built-in method __repr__().
return statement
A statement which terminates the execution of the function body in which it occurs. If it is followed by an expression, then the function call returns that value. Otherwise, the function call returns None.

scope
The scope of a variable is the set of statements in which it can be referenced. For a parameter, it is the body of the function in which the parameter is declared. For a local variable, it is from its initial assignment statement until it is deleted, or the end of the function body.
self
A keyword that is used to reference the object associated with the current method call. Every method header must have self as its first parameter.
sequence
A value that is an ordered list of other values. The contents of a sequence can be accessed by bracket notation. For a sequence seq, seq[2] is the element in position 2; seq[1:5] is the slice (subsequence) from positions 1 to 5 (including 1, but not including 5).
setter
An invisible method that is associated with a property. It is defined with the following syntax:

    @<property>.deleter
    def <property>(self):


Any assignment statement to a property is converted to a method call on the setter.
specification, of a class
A description of what a class is for. Generally speaking, it indicates what an instance of the class represents. Class specifications are typically written using docstrings.
specification, of a function
(See function specification).
String, class
The values of class String are objects that contain sequences of characters. They behave like sequences in that we can get elements and slices of them using bracket [] notation.
subclass
Class SC is a subclass of class C, and C is the superclass of SC, if the definition of class SC has an extends clause extends C. This means that every instance of class SC inherits all the components of class C (Gries/Gries, pp. 141–162).
super
A keyword that is used in two ways. First, to call an inherited function instead of an overriding one. Second, to call a constructor of a superclass.
superclass
(See subclass).

type
A set of values together with operations on them. The type of a value can be determined via the type() function.

variable
A name with an associated value. Sometimes viewed as a named box with the value in the box. In Python, there are four kinds of variable: parameter, local variable, field, and a global variable.

wider type
A type to which Python will perform a cast automatically; that is Python will automatically cast from a narrower type to a wider type. See narrower type for more information.

Acknowledgments

This document was originally written by David Gries to accompany the Java version of CS1110. It has since been adapted to Python by Walker White.