nup_logo

Machine Learning with Python

Lecture 3. Python slices, module Collections, refcount


Alexander Avdiushenko
October 10, 2023

Indexes of Python list

slicing

Slices

List comprehensions

Unpacking in Python

Unpacking in a loop

Comprehensions

collections

Collections → defaultdict

Collections → deque

Collections → deque → complexities

Insertion at the beginning/end, in the middle? O(1) and O(n) respectively
Access by index at the beginning/end/middle O(1)
Search for an element O(n)

Collections → OrderedDict

Collections → Counter

More interesting examples in Python

gc_python_meme
Fun with sys getrefcount post
Python doc about the garbage collector (gc)
  1. Reference count algorithm — deletion is triggered as soon as the count hits 0, but it brings many problems (circular references, thread locking, memory and CPU overhead)
  2. Additional garbage collector — gc module. Three generations of an object, each with its own heuristics when launched

Never repeat this at home!

LOAD_GLOBAL namei — Loads the global named co_names[namei] onto the stack
LOAD_CONST consti — Pushes "co_consts[consti]" onto the stack
LOAD_FAST var_num — Pushes a reference to the local co_varnames[var_num] onto the stack
CALL_FUNCTION argc — Calls a function. The low byte of argc indicates the number of positional parameters, the high byte the number of keyword parameters. On the stack, the opcode finds the keyword parameters first. For each keyword argument, the value is on top of the key. Below the keyword parameters, the positional parameters are on the stack, with the right-most parameter on top. Below the parameters, the function object to call is on the stack
POP_TOP — Removes the top-of-stack (TOS) item

Thank you for your patience!