breakpoint(): Python’s version of MATLAB’skeyboard()commandcallable(): Like MATLAB’sisfunction()but it really checks if there’s a__call__methodgetattr()/hasattr(): MATLAB’sgetfield()/isfield(). The 3rd parameter ofgetattr()is a shortcut to spit out a default if there’s no such field/attribute, which MATLAB doesn’t haveglobals()/locals(): more convenient than MATLAB because the whole workspace (current variables) are accessed as a dictionary in Python by callinglocals()andglobals()id(): memory address of the item where the variable (reference) is pointing to. Think of it as&xin C. MATLAB doesn’t do alias or pointers.isinstance(): MATLAB’sisa()next(): Python favors not actually computing the values until needed so instead it offers a generator (forward iterable) function that spits out one value at each time you kick it withnext()and you can’t go back. MATLAB does not do iterators.chr()/ord(): analogous to MATLAB’schar()/double()cast for characters- Python’s exponentiation is
**, not^like most other languages (C does not have exponentiation symbol, and^was used for xor) print(..., flush=false)allows a courtesy flushrepr(): MATLAB’s version ofdisp(), also overloadable standard interfaceslice(): MATLAB’s equivalent ofcolon()special interface
Context Manager
@contextlib.contextmanager decorators basically splits a set-try-yield-finally boilerplate function into 3 parts: __enter__ (everything above yield), BODY (where the yield goes to) and __exit__ (everything below yield), since a with-as statement is a rigidly defined try-finally block, roughly like this:
with EXPR as f: BODY(using f)
__enter__: f=EVAL(EXPR) try: # f isn't evaluated till yield yield f # Goes to BODY finally: __exit__: cleanup(f)
… more to come as I have came across noteworthy cases.
![]()