Final Flashcards
Accumulator
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.
Assert Statement
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>
Attribute
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.
Instance Attributes
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.
Class Attributes
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.
Bottom Up Rule
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.
Call Frame
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.
Call Stack
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.
Class
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.
Class Definition
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>
Constructor
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.
Default Argument
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.
Encapsulation
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.
Exception
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.
Getter
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.
Generator
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.
Global Space
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.
The Heap
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.