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

CS 1110: Introduction to Computing Using Python

Spring 2014

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]

application
An application is another name for a script.
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.
attribute
A variable belonging to either an object or a class. To reference a field, it must be preceded either by a variable containing the object name (for an object attribute) or the class name (for a class attribute). For example, to reference the attribute x inside of an Point object stored in the variable p, we use p.x

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 boolean is a primitive type whose values are True and False. Python refers to booleans as type bool.
bottom-up rule
The rule that indicates that, when looking for or a method or attribute in an object, first check the object folder, and then (if not found) start from the bottom of the class folder and look upward. This rule ensures that an overriding method will be called (if present) rather than an inherited one.

call frame
(See frame for a function call.)
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 and attributes that go in each object of the class.
class attribute
An attribute that is stored in the class folder, not the object folder. Changes to a class attribute affect all instances of that class.
class expression
A class expression is another name for a constructor call.
class folder
The class folder is a special folder (in heap space) that stores any methods or class attributes that are shares by all instances of that class.
class invariant
The collection of meanings and constraints on the attributes 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 part of the docstring specification for the class. The initializer 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 constructor is a function that has the same name as a class. For example, the function Point() is the constructor for the class Point.
constructor call
A constructor call is a call to a constructor function. It as the form <class-name>(<arguments>). For example, Point(1,2,3) is a class expression for the class Point.
A constructor call is evaluated in three steps:
  1. Create an instance of class <class-name>
  2. Execute the constructor __init__(<arguments>)
  3. Return as a value the name of the newly created instance

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 field is a special type of attribute which behaves like any other variable. As with any variable, it is created by an assignment statement. For example, while the Point object p does not normally have an attribute w, the assignment statement
  p.w = 10
will add such a field to p. In this course, almost all attributes are fields.
float
A primitive type whose values are scientific numbers, e.g. 3.46E–4.
folder
A folder is our running analogy for how information is stored in heap space. The following information is stored in folders: In this class, we are primarily interested in object and class folders.
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 function
  2. a program (or instrunction) counter, which indicates the statement to execute next
  3. the parameters of the function
  4. the local variables of the function
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. This body is stored in a folder in heap space. When the method is called, its body is executed.
function call
An expression that ends up executing the function body. It has the following form:

    <function-name>(<arguments>)

It is important to understand how a function 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 a function is 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 space
A region of memory that stores global variables.
global variable
A variable that is defined outside of the body of a function or class definition. Both function names and module names are global variables; they contain the identifier of the folder in heap space that contains their contents.

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.
heap space
A region of memory that stores all of the folders. Anything that is not one of the four primitive types must be stored as a folder in heap space.

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

    import <module>        # encapsulate contents in module folder
    from <moduleimport * # pull everything into global space

It is used to access the global variables, functions, and classes within a module. When we import a module, all module variables (and function names) are stored in a folder; they must be accessed through a variable with the same name as the module. For example, if we use the statement
  import math
then we write math.pi to access the variable pi in this module. On the other hand, if we write
  from math import *
then all of the contents of the module math are dumped into global space. Therfore, we only need to write pi to access the same variable.
initializer
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.
initializer, default
If (and only if) a class definition does not contain a initializer, it uses the one that it inherits from its subclass, which may be the class object.
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. The words instance and object 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
Instantiate means "to create an instance of". Evaluation of a class expression C(...) instantiates class C, producing a new instance of it.
int
Type int is a primitive type whose values are integers.
interactive mode
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 bool True
Type str "Hello World!"
Type str 'Hello World!'
local variable
A variable that is declared within the body of a function or method.

method
A function that is contained within a class definition. Methods are able to access the attributes of the class, in addition to the parameters and local variables that any function can access.
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 a script.

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 attribute
An attribute that is unique to a specific object, and is therefore stored in the object folder.
object, class
The class object is the superest class of them all: any class that does not explicitly extend another class must extend class object.
object folder
The object folder is the folder in heap space that contains any attribute values that are unique to this 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, float, bool, and str. There is also a complex type, but we do not cover this in class.
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 property is a special type of attribute which acts like a variable, but is not a variable. Attempts to use a property are substituted with method calls, according to its setter, getter, and deleter methods. The purpose of properties is to enforce class invariants.

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.
script
A Python script is a program that contains a segment of code like this:

    if __name__ == "__main__":

Scripts can be run outside of the interactive Mode. To run an script, type python <application name>  at the OS command shell. When a script is run, it will execute all of the code indented under the if-statement above.
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
The values of type str are sequences of characters. Their literals are indicated by quotation marks. 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.
super
The keyword super is used that is used call an inherited method instead of an overriding one.
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, attribute, 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.


Course Material Authors: D. Gries, L. Lee, S. Marschner, & W. White (over the years)