Skip to content
academia.sh

Course Intermediate

Object-Oriented Python and Types

By the end of this course

Start course

01

Classes and the Data Model

Binding of attributes and methods, multiple inheritance's resolution order, visibility declared through naming, representation and comparison protocols, controlling attribute access, short definitions for value objects, and nominal versus structural contracts.

  1. 01 Class and Instance An attribute written in the class body is shared as 1 object across three instances; one written in the instance body produces 3 separate objects. `self` is not a keyword — it is the instance carried by the bound method built at the moment of access.
  2. 02 Inheritance and Method Resolution Order The body of four capabilities spreads across three separate classes in inheritance, and `Document` writes 0 of them in its own body; `Wrapper` writes all 4 in composition. The `format` method, whose body is written in `Record`, prints `<Signed>`.
  3. 03 Encapsulation Conventions 3 of four access forms give a value, and the access the name blocks is 0; a double underscore is not a lock but a name change, and it opens 2 entries instead of 1 for two classes' same name.
  4. 04 Special Methods A class writing no special method joins 5 of eleven syntax forms but never answers any of them itself; writing `__eq__` empties out the `__hash__` name, and a single method can close two syntax forms at once.
  5. 05 Properties and Descriptors The same seven accesses run 0 bodies on a plain field and let an invalid value through; an intervening definition runs 7 bodies and drops it. When a descriptor holds data on itself, three instances end up sharing one value.
  6. 06 Data Classes A short definition of four source lines writes the same 4 special methods a hand-written ten-line class writes; the ordering option raises it to 8, freezing to 6, both together to 10, and on the frozen class 3 of five operations fall.
  7. 07 Abstract Base Classes and Protocols 1 of three candidates passes the ancestry test, 3 pass the method test, and 3 answer the call; in the two tests' blind spots, 3 of three candidates are accepted but only 1 answers the call.

02

The Type Layer

Function and variable annotations, reusable type definitions, what the checker finds and cannot find, and schema-based runtime validation.

  1. 01 Type Hints The annotation is written into the code, and the interpreter stores it without checking it: four of five calls are violations and the number that errors at runtime is zero, yet every annotation remains readable at run time.
  2. 02 Generics and Type Variables Four notations of the same function give different results at four call sites: the unannotated notation misses two violations, the loose notation finds both but produces two false alarms, the generic notation finds both and produces no false alarm at all.
  3. 03 Static Type Checking The checker modeled within the lesson finds two of five calls' type violations and cannot see two domain violations; in exchange, it sees all three of the source's call sites, while run time only reaches one.
  4. 04 Runtime Validation Three layers see the four violations separately: runtime catches 0, the checker 2, the domain rule 2, and all four only become visible when two layers are used together; schema-based validation catches all four, but only on the calls it reaches.

Start typing to search.

↑↓ Esc navigate · open · close