python tips for myself

Joined November 2022
22 Photos and videos
f-string is becoming a dominant way to format strings in Python thanks to its powerful and concise syntax. But do you know backslashes may not appear inside the curly braces {}. Here is an sample and explanation.
1
120
In CPython, due to the GIL, only one thread can execute Python code at once. #Python
1
95
A key difference between three class builders of #Python “data class” is that collections.namedtuple and typing.NamedTuple build tuple subclasses, therefore the instances are immutable. By default, @ dataclass produces mutable classes.
1
125
Python uses data buffer to temporarily store data before it is sent to output device.
1
95
Concurrency provides a way to structure a solution to solve a problem that can be (not necessarily) be parallelizable. Parallelism is just a special case of concurrency. #Python
1
73
After #Python 3.8, you can use assignment expression to assign and return a value in the same expression, which is also called walrus operator (:=) as it resembles the eyes and tusks of a walrus on its side.
1
1
walrus:
1
The walrus operator was introduced in PEP 572. peps.python.org/pep-0572/

62
In #Python interviews, many people are asked what are the differences between lists and tuples, and mutability is certainly the most mentioned. Beware that although the content of a tuple remains immutable, the referenced object can indeed change.
1
1
You can make a list act as a stack or a queue (if you use .append and .pop(0), you get FIFO behavior). But inserting and removing from the head of a list (the 0-index end) is costly because the entire list must be shifted in memory.
1
62
The class collections.deque is a thread-safe double-ended queue designed for fast inserting and removing from both ends.
70
SciPy is a library, written on top of NumPy, offering many scientific computing algorithms from linear algebra, numerical calculus, and statistics. SciPy is fast and reliable because it leverages the widely used C and Fortran codebase from the Netlib Repository .
2
1
2
73
Most NumPy and SciPy functions are implemented in C or C , and can leverage all CPU cores because they release Python’s GIL (Global Interpreter Lock).
46

Replying to @pythonnotes
For advanced array and matrix operations, NumPy is the choice, which is the reason why Python became mainstream in scientific computing applications. NumPy implements multidimensional, homogeneous arrays and matrix types that hold not only numbers but also user-defined records.
50
In #Python, if you want a more efficient and compact way to store homogeneous primitive data, use Python array, something like C language array.
2
For advanced array and matrix operations, NumPy is the choice, which is the reason why Python became mainstream in scientific computing applications. NumPy implements multidimensional, homogeneous arrays and matrix types that hold not only numbers but also user-defined records.
84
Arrays support all mutable sequence operations (including .pop, .insert, and .extend), as well as additional methods for fast loading and saving, such as .frombytes and .tofile.
39
Everything in #Python is an object, including functions and class definitions and basic values like integers, floats, Booleans, and strings.
49
In Python, static methods aren’t bound to an object and are often used to define utility methods or group functions that have some logical relationships in a class, which allow us to do overriding in subclass and ease the readability of the code.#python
1
1
This post is a good explanation to static methods in Python: julien.danjou.info/guide-pyt…

When a #Python function is defined, the Python bytecode compiler determines how to fetch a variable that appears in it based on some variable lookup logic.
To specify keyword-only arguments when defining a function, name them after the argument prefixed with *. If you don’t want to support variable positional arguments but still want keyword-only arguments, put a * by itself in the signature. #Python #pythonprogramming
In #Python, first-class functions can be helpful for programming in a functional style, of which one hallmark is using higher-order functions, such as `map`, `filter`, `reduce`. But, in same cases, list comprehensions or generator expressions can be better choices.
Liskov Substitution Principle (LSP) is defined 'subtype-of' in terms of supported operations, which is called behavioral subtyping.
2
Duck typing is popular in Python, JavaScript, and Ruby, only enforced at runtime, and more flexible than nominal typing that adopted by C , Java, and C#.