Skip to content

Week 7 Notes

  • Lab 06: Nonlocal, Iterators & Generators (04/07/22)

    • Trick: yield from map(fn, <iterable>) to perform calculation on elements of the input <iterable>
  • Reading Ch. 2.5: Object-Oriented Programming (14/7/2022)

    • Every object has a type, called its class
    • Class: a template for all objects whose type is that class
      • Every object is an instance of some class
      • Specifies the attributes and methods shared among that class’ objects
      • CamelCase convention
      • class <name>:
        • <suite>
    • Instantiating: creating a new object instance of a class
    • Attribute of an object: a name-value pair associated with the object
      • Accessible via dot notation
      • Specific to a particular object → Instance attributes/fields/properties/instance variables
      • If starts with an underscore, it should only be accessed within methods of the class itself, rather than by users of the class
      • Class attributes/class variables/static variables: shared attribute values across all objects of the same class
      • Changing the class attribute changes instances without like-named instance attributes, but the existing instance attributes are unaffected
    • Methods: functions that operate on the object or perform object-specific computations
      • Constructor: a method that initializes new objects
        • def __init__(self, <input>):
        • self is bound to the newly created object, <input> is bound to the argument passed to the constructor
    • Identity: Each new object instance has its own instance attributes, independent of other objects’
      • Binding an object to a new name does not create a new object
    • Dot expressions:
      • <expression>.<name>
        • Evaluate <expression> → yields object of the dot expression
        • <name> is matched against that object’s instance attributes
        • If none is found, <name> is looked up in the class → yields a class attribute value
        • Otherwise look up the name in the base class, if there is one
        • In case that matched value is a function, a bound method is returned
          • Instance attributes > class attributes
    • Python distinguishes between functions and bound methods
      • As an attribute of a class, a method is a function
      • But as an attribute of an instance, it is a bound method (Because it has that instance automatically bound to the parameter self when it is called)
    • Inheritance: Similar classes differ in their amount of specialization
      • Subclass (child class) inherits the attributes of its base class (parent class, superclass), but may override certain ones, including methods. We only specify what is different, the rest stays
      • Represents is-a relationship between classes
      • Python supports multiple inheritance
      • class <name>(<base class 1 name>, <base class 2 name>):
    • Calling ancestors: Overridden attributes are still accessible via class objects
      • <ancestor class name>.<attribute name>
    • Interfaces: a collection of attributes of different types of objects that share the same attribute names
      • In Python, interfaces are implemented implicitly. Any object with the appropriate names implements an interface
      • Make assumptions about attribute names rather than the object type → More robust code (Use object abstraction)
    • Inheritance graph:

    Inheritance graph

    • Each arrow points from a subclass to a base class
    • For ambiguous reference, Python resolves names from left to right then upwards
  • Lecture 17: Objects (14/07/2022)

  • Lecture 18: Inheritance (14/07/2022)

    • Inheritance is best for representing is-a relationships
    • Composition is best for representing has-a relationships
    • Multiple inheritance is complicated. Avoid it
  • Discussion 06: Nonlocal, Iterators & Generators (17/06/22)

  • Guerrilla 02: Data Abstraction, Trees, Nonlocal, Iterators & Generators (20/07/22)

    • Min-heap tree: every node’s value is less than or equal to the values of all of its children
    • Added entries to Essential Coding Patterns: BFS, Min-heap Trees
  • Homework 05: Linked Lists, Trees, and Generators (20-21/07/22)

    • Added entries to Essential Coding Patterns: DFS, Binary Search Trees
    • Valid BST
      • Each node has at most 2 children nodes
      • A node with 1 branch can consider that branch as left or right
      • For every node, every left node >= node label & every right node <= node label
      • A leaf is a BST
      • Each branch is a BST
  • Project 3: Ants Vs. SomeBees (23/07/22)

    • Do not mutate list while iterating through it
      • But if you have to do it, you can iterate through reversed(lst) or through a copy of the list: lst[:] or list(lst)
  • Reading Ch. 2.7: Object Abstraction (26-27/07/22)

    • Generic/polymorphic function: a central concept in object abstraction, a function that can accept values of different types
      • Shared interfaces
        • A set of shared attribute names along with their behaviors
      • Type dispatching
        • Inspect the type of arguments they receive → execute type-appropriate code
        • Use class attribute .type_tag to differentiate user-defined types
      • Type coercion
        • Different data types are not completely independentm and objects of one type may be viewed as being of another type
        • Coericion functions transform an object of one type → equivalent object of another type
    • Python stipulates that all objects should produce two string representations: one human-interpretable text and one Python-interpretable expression
      • str() → human-readable string; repr() → Python expression that evaluates to an equal object
      • eval(repr(object)) == object
      • The result of calling repr() on the value of an expression is what Python prints in an interactive session or a description
      • repr() needs to be a polymorphic function → calling repr() invokes a method called __repr__() on its argument
        • repr(<object>) == <object>.__repr__()
        • str(<object>) == <object>.__str__()
    • Special methods
      • __init__(): automatically invoked whenever an object is constructed
      • __str__(): automatically invoked when printing
      • __repr__(): invoked in an interactive session to display values
      • __bool__(): if an object defines the __bool__() method, then Python uses that to determine its truth value, otherwise it uses the default bool()
      • __len__(), __getitem__(): sequence operations, used to get length and select element of sequences
      • __call__(): defined objects that can be called like functions will invoke this
    • Python can compute attributes from zero-argument functions without using call expression syntax → @property decorator
  • Lecture 19: Representation (27/07/22)

    • str() and repr() IGNORES the instance attribute, instead looks for __str__() and __repr__() in the class first, if none is found, uses default str() and repr()