Ben Chuanlong Du's Blog

It is never too late to learn.

Python Tips and Traps

Misc

https://docs.python-guide.org/writing/gotchas/

https://stackoverflow.com/questions/101268/hidden-features-of-python

  1. The int type in Python 3 is unbounded, which means that there is no limit on the range of integer numbers that an int can represent. However, there are still various integer related limits in Python 3 due to the interpreter's word size (sys.maxsize) (which is the same as the machine's word size in most cases). This number is the size of the largest possible list or in-memory sequence.
In [1]:
x = 1111111111111111111111111111111111111111111111111111111111111111111111111111
type(x)
Out[1]:
int
  1. Use type annotation to make your code more readable and easier to understand.

Functions and Classes

  1. Restrict the types of objects that your function/method can be applied to and throw a (ValueError) exception when a wrong type is provided. This helps minimize surprisings. If you must make a function accepts 2 (or more) customized types of objects, it is best to make them have similar duck types. For example, one object has members mem1 and mem2 (which are used in the function), instead of representing the 2nd type of object as a tuple (of 2 elements) it is better to define a class to match interfaces of the first type of object so that implementation of the function is simple and concise. Noticce that dataclass is a good way to implement simple classes.

  2. AVOID returning objects of different types from a Python function/method.

  3. Almost all modern programming languages follow the convention of not returnting anything (or in another words, retun void, None, etc.) from a mutator method so that you cannot chain on a mutator method. Functional programming languages enough chaining on methods as they often have immutable objects and the methods return new objects rather than changing the original objects.

  4. Python functions (except lambda functions) do not automatically return value unlike functional programming languages. Forgotting a return statement is a common mistake in Python.

  1. You cannot use return to return result from a generator function. Instead a return behaves like raising a StopIteration. Please see this issue for more discussions.

https://jeffknupp.com/blog/2018/10/11/write-better-python-functions/

https://softwareengineering.stackexchange.com/questions/225682/is-it-a-bad-idea-to-return-different-data-types-from-a-single-function-in-a-dyna

https://stackoverflow.com/questions/1839289/why-should-functions-always-return-the-same-type

https://treyhunner.com/2018/04/keyword-arguments-in-python/

Operator Precedence

  1. According to Python Operator Precedence, the ternary expression if - else has a very low precedence. However, it has higher precedence than the tuple operator ,. It is suggested that you always use parentheses to make the precedence clear when you use the ternary expression in a complicated expression. Below is an example illustrating the precedence of the ternary expression.
In [2]:
update = {
    "status": "succeed",
    "partitions": 52,
    "size": 28836,
    "end_time": 1563259850.937318,
}
[
    key + " = " + f"{val}" if isinstance(val, (int, float)) else f"'{val}'"
    for key, val in update.items()
]
Out[2]:
["'succeed'",
 'partitions = 52',
 'size = 28836',
 'end_time = 1563259850.937318']
In [3]:
update = {
    "status": "succeed",
    "partitions": 52,
    "size": 28836,
    "end_time": 1563259850.937318,
}
[
    key + " = " + (f"{val}" if isinstance(val, (int, float)) else f"'{val}'")
    for key, val in update.items()
]
Out[3]:
["status = 'succeed'",
 'partitions = 52',
 'size = 28836',
 'end_time = 1563259850.937318']

Python Doc

https://docs.quantifiedcode.com/python-anti-patterns/index.html

Comprehensive Python Cheatsheet

Environment Variable

Please refer to Dealing With Environment Variables in Python for detailed discussions.

Programming Skills

  1. Python varadic args can mimic function overloading

  2. Python eval

  3. *args and **kwargs. Arguments after * must be called by name.

  4. sys.stdout.write, sys.stdout.flush, print

  5. Global variables in a Python module are readable but not writable to functions in the same module by default. If you want to write to a global variable in a function (in the same module), you have to declare the global variable in the method using the keyword global. For example, if x is a global variable and you want to write to it in a method, you have to declare global x at the beginning of the method.

Numerical

  1. Division is float division in Python 3 which is different from Python 2. If you want integer division, use the // operator.

Misc

  1. Keys for set and dict objects must be immutable in Python (and the same concept holds in other programming languages too). Since a list in Python is mutable, it cannot be used as a key in set and dict objects. You have to convert it to an immutable equivalence (e.g., tuple).

  2. Use sys.exit(msg) to print error message and quit when error happens

  3. Get the class name of an object.

     type(obj).__name__

File System

  1. os.mkdir acts like mkdir in Linux and os.makedirs acts like mkdir -p in Linux. Both of them throw an exception if the file already exists.

  2. Use execfile(open(filename).read()) to source a file, variables defined in 'filename' are visible, however, imported packages are invisible to the script running execfile

Encoding

ord unichar return ascii number of characters chr return a string from a ascii number

Syntax

  1. Python expression is calculated from left to right.

  2. You can use a dict structure to mimic switch in other programming languages. However, it is kind of evil and has very limited usage., You should avoid use this. Just use multiple if ... else ... branches instead.

  3. : has higher priority than arithmetic operators in Python, which is opposite to that in R.

  4. return v1, v2 returns a tuple (v1, v2). And if a function f returns a tuple (v1, v2, v3), you can use v1, v2, v3 = f()

  5. Stay away from functions/methods/members starting with _. For example, you should use the built-in function len to get the length of a list instead of using its method __len__.

  6. Python does not support ++, -- but support +=, -+, etc.

Encryption

In [ ]:
 

Comments