click below
click below
Normal Size Small Size show me how
Programming #3
From all lessons in "Building Program Structure"
| Term | Definition |
|---|---|
| Sequential/Procedural Design | Centered around functionality. A mathematical view of a program. Programs transform information. This is how functions and algorithms are made. |
| Object-Oriented design | Centered around information. An engineering view of a program. Emulates how the real world is built |
| Classes | Blueprint for creating objects. Custom type. Always represents one thing (ex. Employee). Has attributes(variables) and methods(functions) |
| New objects from a class | billy = Student() |
| Accessing object attributes | Use dot notation to access the attribute. ex: student1.name |
| Methods | Declared and defined in a class. Statements are related to the object and work with the object's attributes. Passes self - def method(self, parameter): |
| Initializer | Each time an object is instantiated from a class, this method runs. The default initializer is hidden and does nothing. def __init__(self): |
| Customized initializer | Pass parameters into __init__ using class name when object is instantiated. in class - def __init__(self, species): self.species = species / in program - pet1 = Pet("cat") |
| Encapsulation | Hiding how the inside of a class works from the rest of the source code, only providing access to what's necessary |
| Protected attributes | For internal use only. _species = "" |
| Private attributes | For internal use and even harder to access. __species = "" |
| Accessor method | Purpose is to return (get) the value of an attribute. Same return type as the attributes whose value is being returned. No parameters other than self. Start with "get". def getName(self): return self.name |
| Mutator method | Purpose is to change (set) the value of an attribute. No return type. One parameter of the same type, as well as self. Start with "set". def setName(self, newValue): self.name = newValue |
| Has (Association) | Attributes are used to implement a the relationship. An object that is "stored" as an attribute can be used by any method within its associated object. Permanent relationship, existing while the object exists |
| Uses | Simplest and weakest relationship. An object is provided to the client object for temporary use. Temporary relationship, existing while one object uses another |
| Is (Inheritance) | Deriving a new class from a base class. The parent class is a generalization of the child class. The child only inherits from one parent |
| Overriding | A child can create custom versions of attributes and methods already declared in the parent class |
| Exception | An event that occurs during the execution of a program that is unexpected by the program code. Also the name of the parent class. Exceptions can get caught along the call stack, so they can occur in one method and be caught in another |
| ValueError | Ex. trying to transform "abc" into a number |
| NameError | Using a variable, method, or type that has not been declared |
| TypeError | An operation or function is applied to an incompatible type |
| IndexError / KeyError | Accessing array elements or keys from locations that do not exist |
| ArithmeticError | Base class for all arithmetic error such as OverflowError, ZeroDivisionError |
| RecursionError | The call stack has reached its memory limit, usually due to infinity recursion (method calling itself) |
| Exception handling | Handles an unwanted event, an exception so that the program code still makes sense to the user |
| try-except | Executed in order. Order from more-specific to less |
| Throwing/raising exceptions | Only use if something is truly wrong. Doesn’t replace an if statement. Exception shouldn't appear in "normal program flow". Do it if a method can’t do what it's supposed to. After throwing an exception the method is interrupted and does not return a value |
| raise | When notifying of an exception, raise one. It is similar to "break", not running any code afterwards. raise ValueError("That's not a good number to use") |
| finally | Executes whether the code raises an exception or not |
| User defined errors | A class that derives from Exception. It doesn't always need specific code, just "pass" is okay. It can define additional properties, override virtual methods and properties, and define specific constructors. |
| Instance field variables | Declared in a class to store data/info as an attribute. Each object can store a different value |
| Static field variables | Value does not change from object to object. Tied to the class, not the object. Shared by objects of the same class. Preface with s_. Not added to the object self, access it with [class name].[field name] |
| Class methods | Methods shared by all objects of the same class. Have access to static field variables but not instance field variables. First parameter is cls, not self. Use the @classmethod decorator. Called using [class name].[class method name] |
| Static methods | Methods shared by all objects of a class. No access to instance or static fields. Used for utility functions that don't rely on class or instance state. Use the @staticmethod decorator. Called using [class name].[static method name] |