1/27
Looks like no tags are added yet.
Recursion Process
(1) Solve problem on small data
(2) Break up the problem and solve it (recursively) on the two halves
(3) Combine the answers back together to get the final answer
Abstraction
When one class pretends to be another through operator overloading or familiar looking methods. Ie. using methods len, getitem, and setitem methods to make your class act like a list. Helps a user work with a complicated concept (like image processing) by presenting as easy-to-understand form.
Attribute
Variables that are stored inside of an object.
Instance attributes belong to an object and are created by assignment statement like objectname.attribute = x, or created in the class initializer. Class attributes belong to the class, created by assignment statement like classname.attribute = “donut” (goes to all instances in the class) or attribute = x in the class definition that is outside of a method definition.
Enforce invariants on attributes is impossible as any value can be stored in an attribute at any time. We make attributes hidden (by starting their name with an underscore), and replacing them with getters and setters to enforce invariants.
Bottom-Up Rule
Python rule to determine which attribute or method definition to use (when attribute used in an expression or the method is called).
First check object folder, then class folder for this object, then child class to parent class until it finds it. If Python reaches the superest class folder for object 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.
Class
A class is any type that is not built-in to Python. A value of this type is called an object.
Class definition
Blueprint for the objects of the class, all have same components: attributes (differing values) and methods. The only difference between objects is the values of their attributes.
In Python, class definitions have the following form:
class classname(superclass):
<spec>, <getters and setters> <initializer def> <method defs>In most cases, we use the built-in class object as the super class.
Constructor
Function that creates an object for a class, same name as class.
When called, constructor: creates an empty object folder, puts the folder into heap space, executes init → passes the folder name parameter self, passes other arguments in order, executes commands in init body. When done with init it returns the object (folder) name as final value of expression, handled automatically.
Default Argument
Value automatically given to a parameter if user doesn’t provide argument for function or method.
Ie. def foo(x,y=2,z=3) , required parameters must be first, default arguments after.
Function calls foo(1), foo(1,0), foo(1,0,0), and foo(1,z=0) all legal.
Encapsulation
Hiding parts of your data and implementation users don’t need to access.
Includes restricting access to attributes via getters and setters, and hidden methods (helpers).
Makes it easier for developer to make changes in their code without breaking the code of users who wrote code based on your class.
Getter
Method that returns the value of an instance attribute (of the same name) when called, so user can access the attribute without permission to change it.
Global Space
Area of memory that stores any variable that is not defined in the body of a function. Also includes function names and modules names, and other global variables. Global variables remain until explicitly erased or quit Python.
The Heap
The heap is the area of memory that stores mutable objects (e.g. folders). It also stores function definitions, the contents of imported modules, and class folders.
Folders in the heap remain until explicitly erased or quit Python. You cannot access the heap directly, access with variables in global space or in a call frame that contains the name of the object in heap space.
Immutable Attribute
Hidden attribute that has a getter, but no setter. This implies that a user is not allowed to alter the value of this attribute. It is an important part of encapsulation.
Implementation
An implementation is a collection of Python code for a function, module, or class that satisfies a specification. Class implementation includes all methods bodies as well as any hidden attributes or methods, module implementation similar.
Inheritance
If the class is a subclass, then any method or attribute is inherited from the superclass, even if that method/attribute was not explicitly mentioned in the subclass definition.
Interface
The interface is the information that another user needs to know to use a Python feature, such as a function, class, or module. The simplest definition for this is any information displayed by the help() function.
Function interface: specification and the function header.
Class interface: class specification and list of all unhidden methods and their specifications.
Instance
Synonym for an object. An object is an instance of a class.
Invariant
Statement about an attribute that must always be true. Can be like a precondition and prevent certain values from being assigned to the attribute.
Invariant can also be a relationship between multiple attributes, ie. if x attribute altered, the other attributes must be altered to match.
is
is operator works like == but compares folder names (same/diff folder), not contents.
The meaning of the operator is can never be changed, different from == which special operator/dunder method eq determines.
If == is used on an object that does not have a definition for method eq, then == and is are the same (it will check folder name).
isinstance
The function call isinstance(obj,C) returns True if object obj is an instance of class C (subclasses return True as well).
This is different than testing type(obj) == C which checks for exact match.
Method
Functions stored inside of a class folder (indented inside). Called by placing the object variable (passed to method def as argument self) and a dot before the function name. Hence all method definitions must have at least one parameter (self).
Mutable Attribute
A mutable attribute is a hidden attribute that has both a getter and a setter. This implies that user is allowed to alter the value of this attribute, as long as the invariant is not violated.
Object
Value whose type is a class. Objects typically contain attributes (can potentially modify) and methods.
Operator Overloading
Operator overloading is how 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” based on type of the objects involved. 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/dunder method in the class definition.
Overriding a Method
In a subclass, one can redefine a method that was defined in a superclass aka overriding. In general, the overriding method is called but to call an overridden method of the superclass, use the notation super().method(…)
If desired method is in a class other than the immediate parent, use
super(self, childclass).method(…), childclass being immediately under the class you want to access. Or grandparent_owner.method(self), grandparent method acting as a function.
Setter
Setter can change the value of an instance attribute (of the same name) when called, purpose is to enforce invariants (assert statements before assignment to attribute). The docstring of the setter typically mentions the invariants as a precondition.
Subclass
A subclass D is a class that extends another class C. An instance of D inherits (has) all the attributes and methods that an instance of C has, as well as its own.
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.
class Image(object): #for example