structure updates
This commit is contained in:
81
tech_docs/python/advanced_python.md
Normal file
81
tech_docs/python/advanced_python.md
Normal file
@@ -0,0 +1,81 @@
|
||||
For an advanced programmer new to Python, understanding Python's unique idioms, "Pythonic" ways, and potential gotchas is essential to write efficient, readable, and Pythonic code. Here's a compiled list of Pythonisms, idioms, and gotchas to look out for:
|
||||
|
||||
### Pythonisms and Idioms
|
||||
|
||||
1. **List Comprehensions**: Compact way to process all or part of the elements in a sequence and return a list with the results.
|
||||
```python
|
||||
squares = [x**2 for x in range(10)]
|
||||
```
|
||||
|
||||
2. **Dictionary Comprehensions**: Similar to list comprehensions but for dictionaries.
|
||||
```python
|
||||
square_dict = {x: x**2 for x in range(10)}
|
||||
```
|
||||
|
||||
3. **Using Underscore for Unused Variables**: It's a Pythonic way to indicate that a variable is intentionally unused.
|
||||
```python
|
||||
for _ in range(10):
|
||||
do_something()
|
||||
```
|
||||
|
||||
4. **Unpacking**: A way to unpack values from a sequence or iterable into variables.
|
||||
```python
|
||||
a, b, *rest = range(10)
|
||||
```
|
||||
|
||||
5. **The Zen of Python**: Accessible by typing `import this`, it's a collection of Python's design principles.
|
||||
|
||||
6. **Using `enumerate()` for Loops**: To get both the index and the value of an item in a list.
|
||||
```python
|
||||
for index, value in enumerate(my_list):
|
||||
print(index, value)
|
||||
```
|
||||
|
||||
7. **Function Arguments**: Python supports default, keyword, positional, and arbitrary arguments.
|
||||
```python
|
||||
def func(a, b, c=5, *args, **kwargs):
|
||||
pass
|
||||
```
|
||||
|
||||
8. **The Walrus Operator (`:=`)**: Introduced in Python 3.8, allows you to assign values to variables as part of an expression.
|
||||
```python
|
||||
if (n := len(a)) > 10:
|
||||
print(f"List is too long ({n} elements)")
|
||||
```
|
||||
|
||||
### Gotchas
|
||||
|
||||
1. **Mutable Default Arguments**: Default argument values are evaluated only once at function definition time, which means that modifying a default argument will affect all subsequent calls to the function.
|
||||
```python
|
||||
def append_to(element, to=[]):
|
||||
to.append(element)
|
||||
return to
|
||||
```
|
||||
|
||||
2. **Dynamic Typing**: While flexible, it can lead to unexpected behaviors if not carefully managed, especially when coming from statically-typed languages.
|
||||
|
||||
3. **Indentation**: Python uses indentation to define blocks, making code readability a part of the syntax. However, incorrect indentation can lead to `IndentationError` or unexpected behavior.
|
||||
|
||||
4. **`==` vs `is`**: `==` checks for equality, while `is` checks for identity. This distinction is crucial for understanding mutable and immutable objects.
|
||||
```python
|
||||
a = [1, 2, 3]
|
||||
b = a
|
||||
print(a == b) # True
|
||||
print(a is b) # True
|
||||
c = a.copy()
|
||||
print(a == c) # True
|
||||
print(a is c) # False
|
||||
```
|
||||
|
||||
5. **Looping Pitfalls**: Modifying a list while iterating over it can lead to unexpected behavior. Use slicing or create a new list instead.
|
||||
|
||||
6. **Global vs Local Variables**: Variables declared inside a function are local unless explicitly declared global.
|
||||
|
||||
7. **Floating Point Arithmetic**: Like many languages, Python's floating-point numbers can have rounding errors.
|
||||
```python
|
||||
print(0.1 + 0.2 == 0.3) # False
|
||||
```
|
||||
|
||||
8. **GIL (Global Interpreter Lock)**: Python's GIL means that only one thread can execute Python bytecodes at a time. This can be a gotcha for CPU-bound multithreading applications.
|
||||
|
||||
Understanding these Python-specific idioms and gotchas is crucial for leveraging Python's strengths while avoiding common pitfalls, ensuring your transition to Python is smooth and productive.
|
||||
Reference in New Issue
Block a user