Classic use of Python’s *args: Invoke it with separate int arguments: and *not* with a list containing ints:
Want your Python function to take any number of positional args? Use *args: – Can be any name, but args is traditional– The * is only in the function definition– It’s a tuple– Don’t grab elements by index; iterate over them– It might be empty– Pronounce it “splat args”
How are arguments assigned to parameters when calling a Python function? There are two ways: – Positional, based on order– Keyword, i.e., name=value, assigned by name Want to mix and match? Fine, but all positional must come before all keyword:
Every import in Python executes the module, from start to finish. Meaning? Big modules slow down your program’s startup time. Coming in Python 3.15 this October: “lazy import”, which delays the load until a name is used: lazy import MODULElazy from MODULE import NAME
Mutable builtins in Python cannot be dict keys. But your (mutable) classes can!
Want to include a dynamic value in a multi-line Python string? That would require both an f-string and a triple-quoted string. Good news: Python supports this!
Want to include a newline in a Python string? Use \n: That’s fine for small strings. But for longer ones, use a triple-quoted string: BTW, the created strings are the same:
A classic Python mistake: What’s wrong with this code? The string uses ” as delimiters. But it also contains a ‘ inside. Solutions:
Want to read a non-text file with Python? Specify “rb”, for “read binary,” to avoid errors. What is t? A *byte* string, not a regular text string.
Python tip: Don’t write all 6 comparison methods! Define __eq__ and __lt__, then add @total_ordering: You get >, >=, <= for free!