Final Flashcards

1
Q

Accumulator

A

An accumulator is a fancy name for a variable in a for-loop that stores information computed in the for-loop and which will be still available when the for-loop is complete.
Example: In the for loop
total = 0
for x in range(5):
| total = total + x
the variable total is an accumulator. It stores the sum of the values 0..4.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Assert Statement

A

A statement in the form of
assert <boolean> or
assert <boolean expression, string expression>
If the boolean expression is true, an assert statement does nothing. If it is false, it produces an error, stopping the entire program. In the second version of assert, it uses the string expression after the comma as its error message.</boolean>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Attribute

A

Attributes are variables that are stored inside of an object.
It is impossible to enforce invariants on attributes as any value can be stored in an attribute at any time.
Therefore, we prefer to make attributes hidden (by starting their name with an underscore), and replacing them with getters and setters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Instance Attributes

A

Instance attributes belong to an object or instance. Instance attributes are created by assignment statement that prefaces the object name before the period. They are typically created in the class initializer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Class Attributes

A

Class attributes belong to the class. They are created by an assignment statement that prefaces the class name before the period. They are also created by any assignment statement in the class definition that is outside of a method definition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Bottom Up Rule

A

This is the rule by which Python determines which attribute or method definition to use (when the attribute is used in an expression, or the method is called). It first looks in the object folder.
If it cannot find it there, it moves to the class folder for this object. It then follows the arrows from child class to parent class until it finds it. If Python reaches the folder for object (the superest class of all) and still cannot find it, it raises an error.
If the attribute or method is in multiple folders, it uses the first one that it finds.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Call Frame

A

A call frame is a formal representation of that Python uses when you execute a function call. It contains the name of the function as well as all parameters and local variables. It has also an instruction counter that tracks the next line in the function that is to be executed. A call frame is deleted (e.g. erased) as soon as the call completes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Call Stack

A

The call stack is all of the call frames of the currently executing function calls (e.g. the main function call and all of its helper functions). These call frames are arranged in a stack, with the original function up top, and the most recent function call at the bottom. If the current function calls a helper function, you add a new frame to the bottom. When a helper function completes, you remove the call frame from the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Class

A

A class is any type that is not built-in to Python (unlike int, float, bool, and str which are built-in). A value of this type is called an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Class Definition

A

This is a template or blueprint for the objects (or instances) of the class. A class defines the components of each object of the class. All objects of the class have the same components, meaning they have the same attributes and methods. The only difference between objects is the values of their attributes.
Using the blueprint analogy, while many houses (objects) can be built from the same blueprint, they may differ in color of rooms, wallpaper, and so on.

In Python, class definitions have the following form:
class <classname>(<superclass>):
<class>
<getters>
<initializer>
<method>
In most cases, we use the built-in class object as the super clas</method></initializer></getters></class></superclass></classname>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Constructor

A

A constructor is a function that creates a object for a class. It puts the object in heap space, and returns the name of the object (e.g. the folder name) so you can store it in a variable. A constructor has the same name as the type of the object you wish to create.

When called, the constructor does the following:
* It creates a new object (folder) of the class, which is empty.
* It puts the folder into heap space.
* It executes the initializer method __init__ defined in the body of the class. In doing so, it
– Passes the folder name to that parameter self
– Passes the other arguments in order
– Executes the commands in the body of __init__
* When done with __init__ it returns the object (folder) name as final value of expression.
There are no return statements in the body of __init__; Python handles this for you
automatically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Default Argument

A

A default argument is a value that is given to a parameter if the user calling the function or method does not provide that parameter. A default argument is specified by wording the parameter as an assignment in the function header. Once you provide a default argument for a parameter, all parameters following it in the header must also have default arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Encapsulation

A

Encapsulation is the process of hiding parts of your data and implementation from users that do not need access to that parts of your code. This includes restricting access to attributes via getters and setters, but it also includes the usage of hidden methods as well. This process makes it easier for you to make changes in your own code without breaking the code of anyone who is using your class. See the definitions of interface and implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Exception

A

An exception is an object that stores the stack trace and and error message for when Python crashes. You can create an exception object by calling its constructor, but that will not crash Python. To crash Python, you must combine the exception with a raise statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Getter

A

A getter is a special method that returns the value of an instance attribute (of the same name) when called. It allows the user to access the attribute without giving the user permission to change it. It is an important part of encapsulation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Generator

A

A generator is a special kind of function for creating an iterable. It is defined by placing a yield statement inside the function body. Calling a generator function returns an iterable object that uses this body to produce its elements. You call the next function to iterate through these elements. For example,
for the generator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Global Space

A

Global space is area of memory that stores any variable that is not defined in the body of a function. These variables include both function names and modules names, though it can include variables with more traditional values. Variables in global space remain until you explicitly erase them or until you quit Python.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

The Heap

A

The heap or heap space is the area of memory that stores mutable objects (e.g. folders). It also stores function definitions, the contents of modules imported with the import command, as well as class folders. Folders in the heap remain until you explicitly erase them or until you quit Python. You cannot access the heap directly. You access them with variables in global space or in a call frame that contain the name of the object in heap space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Immutable Attribute

A

An immutable attribute is a hidden attribute that has a getter, but no setter. This implies that a user it not allowed to alter the value of this attribute. It is an important part of encapsulation.

20
Q

Implementation

A

An implementation is a collection of Python code for a function, module, or class)
that satisfies a specification. This code may be changed at any time as long as it continues to satisfy the specification.

In the case of a function, the implementation is limited to the function body. In the case of a class, the implementation includes the bodies of all methods as well as any hidden attributes or methods. The implementation for a module is similar to that of a class

21
Q

Inheritance

A

Inheritance is the process by which an object can have a method or attribute even if that method or attribute was not explicitly mentioned in the class definition. If the class is a subclass, then any method or attribute is inherited from the superclass.

22
Q

Interface

A

The interface is the information that another user needs to know to use a Python feature, such as a function, module, or class. The simplest definition for this is any information displayed by the help() function.

For a function, the interface is typically the specification and the function header. For a class, the interface is typically the class specification as well as the list of all unhidden methods and their specifications. The interface for a module is similar to that of a class.

23
Q

Instance

A

This is a synonym for an object. An object is an instance of a class.

24
Q

Invariant

A

An invariant is a statement about an attribute that must always be true. It can be like a precondition, in that prevents certain types of values from being assigned to the attribute. It can also be a relationship between multiple attributes, requiring that when one attribute is altered, the other attributes must be altered to match.

25
Q

is.

A

The is operator works like == except that it compares folder names, not contents. The meaning of the operator is can never be changed. This is different from ==, whose meaning is determined by the special operator method __eq__. If == is used on an object that does not have a definition for method __eq__, then == and is are the same.

26
Q

isinstance

A

The function call isinstance(ob,C) returns True if object ob is an instance of class C. This is different than testing the type of an object, as it will return True even if the type of ob is a subclass of C.

27
Q

Iterable

A

An iterable type is the type of any value that may be used in a for-loop. Examples include lists string, and dictionaries.

28
Q

Iterator

A

An iterator is an iterable that may only be used once inside of a for-loop. Once it is used, you have to recreated if you want to use it a second time. You can also use the function next to step through the elements one at a time. Files and web pages are examples of iterators in Python.

29
Q

List

A

A list is a mutable sequence that can hold values of any type. Lists are represented as a sequence of values in square braces (e.g. [a1, a2, . . . , an]). A list can also hold other lists as well; this is how Python represents mutli-dimensional lists and matrices. For example, [[1,2],[3,4]] is a 2x2 list in Python.

30
Q

Method

A

Methods are functions that are stored inside of an object. They are define just like a function is defined, except that they are (indented) inside-of a class definition.

Methods are called by placing the object variable and a dot before the function name. The object before the dot is passed to the method definition as the argument self. Hence all method definitions must have at
least one parameter.

31
Q

Object

A

An object is a value whose type is a class. Objects typically contain attributes, which are variables inside of the object which can potentially be modified. In addition, objects often have methods, which are functions that are stored inside of the object.

32
Q

Operator Overloading

A

Operator overloading is the means by which Python evaluates the various operator symbols, such as +, *, /, and the like. The name refers to the fact that an operator can have many different “meanings” and the correct meaning depends on the type of the objects involved.

In this case, Python looks at the class or type of the object on the left. If it is a built-in type, it uses the built-in meaning for that type. Otherwise, it looks for the associated special method (beginning and ending with double underscores) in the class definition.

33
Q

Overriding a Method

A

In a subclass, one can redefine a method that was defined in a superclass. This is called overriding the method. In general, the overriding method is called. To call an overridden method method of the superclass, use the super function as follows.
super().method(…) where method is the name of the method being overridden.

34
Q

Raise Statement

A

A raise statement a statement of the form
raise exception where exception is a expression for an exception object (e.g. either a constructor call to make a new exception, or a variable storing an existing exception). The raise statement immediately crashes Python and stores the current stack trace in the exception object.

35
Q

Scope

A

The scope of a variable name is the set of places in which it can be referenced. Global variables may be referenced by any function that which is either defined in the same module as the global variable, or which imports that module. The scope of a parameter or local variable is the body of the function in which it is defined. We do not worry about the scope of attributes for right now.

36
Q

Sequence

A

A sequence is a type that represents a fix-length list of values. Examples of sequences are lists, strings, and tuples.

37
Q

Setter

A

A setter is a special method that can change the value of an instance attribute (of the same name) when called. The purpose of the setter is to enforce any invariants. The docstring of the setter typically mentions the invariants as a precondition.

38
Q

Specification, Class

A

A class specification is description of the purpose of a class and how to use it. A
class specification typically describes what entity the class is supposed to represent. Class specifications are often written using docstrings and include the class invariant.

39
Q

Specification, Function (or Method)

A

A function specification is a description of what a function should do. It should include (1) preconditions on the arguments, (2) the return value of the function (if it is a fruitful
function, and any other details on what the function does. A function specification is typically written as a docstring comment.

40
Q

Statement

A

A statement is a command for Python to do something. We have seen the following five
statements so far: assignment statements, return statements, assert statements, conditional-statements, and try-except statements. In addition, any procedure may be used as a statement.

41
Q

Subclass

A

A subclass D is a class that extends another class C. This means that an instance of D inherits (has) all the attributes and methods that an instance of C has, in addition to the ones declared in D. In Python, every user-defined class must extend some other class. If you do not explicitly wish to extend another class, you should extend the built-in class called object (not to be confused with an object, which is an instance of a class). The built-in class object provides all of the special methods that begin and end with double underscores.

42
Q

Tuple

A

A tuple is identical to a list except that it is immutable. The contents cannot be removed, expanded, or otherwise altered. Tuples are represented as a sequence of values in parentheses.

43
Q

Try Except Statement

A

This is a statement of the form
try:
<statements>
except:
<statements>
Python executes all of the statements underneath try. If there is no error, then Python does nothing and skips over all the statements underneath except. However, if Python crashes while inside the try portion, it recovers and jumps over to except, where it executes all the statements underneath there.</statements></statements>

44
Q

Type

A

A type is a set of values and the operations on them. The basic types are types int, float, bool,
and str. The type list is like str, except that its contents are mutable.

45
Q

Variable

A

Depending on how you wish to think about it, a variable is a name with associated value or a
named box that can contain a value. We change the contents of a variable via an assignment statement. A variable is created when it is assigned for the first time. We have seen four types of variables in this class: global variables, local variables, parameters, and attributes.

46
Q

Yield Statement

A

A yield statement is a statement which temporarily pauses the execution of a generator. It produces the next value in the iterator associated with the generator.