mirror of
https://github.com/satwikkansal/wtfpython.git
synced 2024-09-11 20:17:08 +03:00
Improve formatting
This commit is contained in:
parent
69da562140
commit
e84eba6fbb
38
README.md
38
README.md
@ -2,7 +2,7 @@
|
||||
|
||||
[![WTFPL 2.0][license-image]][license-url]
|
||||
|
||||
> A collection of tricky Python examples
|
||||
> A collection of nice and tricky Python examples.
|
||||
|
||||
Python, being a high-level and interpreter based programming language which is awesome by design, provides us with many functionalities for programmer's comfort. But sometimes, the outcomes of your code may not seem obvious to a normal Python user at the first sight.
|
||||
|
||||
@ -111,7 +111,7 @@ Probably unexpected output
|
||||
**Note:** All the examples mentioned below are run on Python 3.5.2 interactive interpreter unless explicitly specified.
|
||||
|
||||
|
||||
##### 💡 Explaination:
|
||||
#### 💡 Explaination:
|
||||
* Brief Explaination of what's happening and why is it happening.
|
||||
```py
|
||||
Setting up examples for clarification (if necessary)
|
||||
@ -165,6 +165,7 @@ The midnight time is not printed.
|
||||
Before Python 3.5, the boolean value fo `datetime.time` object was considered to be `False` if it represented midnight in UTC. It is error-prone when using the `if obj:` syntax to check if the `obj` is null or some equivalent of "empty".
|
||||
|
||||
|
||||
---
|
||||
### `is` is not what it is!
|
||||
|
||||
|
||||
@ -243,6 +244,7 @@ Here the integer isn't smart enough while executing `y = 257` to recongnize that
|
||||
* When a and b are set to `257` in the same line, the Python interpretor creates new object, then references the second variable at the same time. If you do it in separate lines, it doesn't "know" that there's already `257` as an object.
|
||||
* It's a compiler optimization and specifically applies to interactive environment. When you enter two lines in a live interpreter, they're compiled separately, therefore optimized separately. If you were to try this example in a `.py` file, you would not see the same behavior, because the file is compiled all at once.
|
||||
|
||||
---
|
||||
### The function inside loop sticks to the same output
|
||||
|
||||
```py
|
||||
@ -296,6 +298,7 @@ Even when the values of `x` were different in every iteration prior to appending
|
||||
```
|
||||
|
||||
|
||||
---
|
||||
### Loop variables leaking out of local scope!
|
||||
|
||||
1\.
|
||||
@ -358,6 +361,7 @@ print(x, ': x in global')
|
||||
> "List comprehensions no longer support the syntactic form `[... for var in item1, item2, ...]`. Use `[... for var in (item1, item2, ...)]` instead. Also note that list comprehensions have different semantics: they are closer to syntactic sugar for a generator expression inside a `list()` constructor, and in particular the loop control variables are no longer leaked into the surrounding scope."
|
||||
|
||||
|
||||
---
|
||||
### A tic-tac-toe where X wins in first attempt!
|
||||
|
||||
```py
|
||||
@ -390,6 +394,7 @@ And when the `board` is initialized by multiplying the `row`, this is what happe
|
||||
|
||||
![image](/images/tic-tac-toe/after_board_initialized.png)
|
||||
|
||||
---
|
||||
### Beware of default mutable arguments!
|
||||
|
||||
```py
|
||||
@ -446,6 +451,7 @@ def some_func(default_arg=[]):
|
||||
```
|
||||
|
||||
|
||||
---
|
||||
### Mutating the immutable!
|
||||
|
||||
This might be obvious for most of you guys, but it took me a lot of time to realize it.
|
||||
@ -477,6 +483,7 @@ TypeError: 'tuple' object does not support item assignment
|
||||
|
||||
* `+=` operator changes the list in-place. The item assignment doesn't work, but when the exception occurs, the item has already been changed in place.
|
||||
|
||||
---
|
||||
### Using a variable not defined in scope
|
||||
|
||||
```py
|
||||
@ -514,6 +521,7 @@ UnboundLocalError: local variable 'a' referenced before assignment
|
||||
```
|
||||
|
||||
|
||||
---
|
||||
### The disappearing variable from outer scope
|
||||
|
||||
```py
|
||||
@ -593,6 +601,7 @@ NameError: name 'e' is not defined
|
||||
```
|
||||
|
||||
|
||||
---
|
||||
### Return return everywhere!
|
||||
|
||||
```py
|
||||
@ -614,6 +623,7 @@ def some_func():
|
||||
- When a `return`, `break` or `continue` statement is executed in the `try` suite of a "try…finally" statement, the `finally` clause is also executed ‘on the way out.
|
||||
- The return value of a function is determined by the last `return` statement executed. Since the `finally` clause always executes, a `return` statement executed in the `finally` clause will always be the last one executed.
|
||||
|
||||
---
|
||||
### When True is actually False
|
||||
|
||||
```py
|
||||
@ -632,6 +642,7 @@ I've lost faith in truth!
|
||||
- Initially, Python used to have no `bool` type (people used 0 for false and non-zero value like 1 for true). Then they added `True`, `False`, and a `bool` type, but, for backwards compatibility, they couldn't make `True` and `False` constants- they just were built-in variables.
|
||||
- Python 3 was backwards-incompatible, so it was now finally possible to fix that, and so this example wont't work with Python 3.x.
|
||||
|
||||
---
|
||||
### Evaluation time disperancy
|
||||
|
||||
```py
|
||||
@ -646,12 +657,13 @@ array = [2, 8, 22]
|
||||
[8]
|
||||
```
|
||||
|
||||
##### 💡 Explaination
|
||||
#### 💡 Explaination
|
||||
|
||||
- In a generator expression, the `in` clause is evaluated at declaration time, but the conditional clause is evaluated at run time.
|
||||
- So before run time, `array` is re-assigned to the list `[2, 8, 22]`, and since out of `1`, `8` and `15`, only the count of `8` is greater than `0`, the generator only yields `8`.
|
||||
|
||||
|
||||
---
|
||||
### Be careful with chained operations
|
||||
|
||||
```py
|
||||
@ -688,6 +700,7 @@ While such behavior might seem silly to you in the above examples, it's fantasti
|
||||
So, `1 < 1` evaluates to `False`
|
||||
|
||||
|
||||
---
|
||||
### a += b doesn't behave the same way as a = a + b
|
||||
|
||||
1\.
|
||||
@ -726,6 +739,7 @@ a += [5, 6, 7, 8]
|
||||
|
||||
* The expression `a + =[5,6,7,8]` is actually mapped to an "extend" function that operates on the object such that `a` and `b` still point to the same object that has been modified in-place.
|
||||
|
||||
---
|
||||
### Backslashes at the end of string
|
||||
|
||||
**Output:**
|
||||
@ -744,6 +758,7 @@ SyntaxError: EOL while scanning string literal
|
||||
|
||||
A raw string literal, where the backslash doesn't have the special meaning, as indicated by the prefix r. What it actually does, though, is simply change the behavior of backslashes so they pass themselves and the following character through. That's why backslashes don't work at the end of a raw string.
|
||||
|
||||
---
|
||||
### Editing a dictionary while iterating over it
|
||||
|
||||
```py
|
||||
@ -776,6 +791,7 @@ Yes, it runs for exactly 8 times and stops.
|
||||
* It runs 8 times because that's the point at which the dictionary resizes to hold more keys (we have 8 deletion entries so a resize is needed). This is actually an implementation detail.
|
||||
* Refer to this StackOverflow [thread](https://stackoverflow.com/questions/44763802/bug-in-python-dict) explaining a similar example.
|
||||
|
||||
---
|
||||
### `is not ...` is different from `is (not ...)`
|
||||
|
||||
```py
|
||||
@ -790,6 +806,7 @@ False
|
||||
- `is not` is a single binary operator, and has behavior different than using `is` and `not` separated.
|
||||
- `is not` evaluates to `False` if the variables on either side of the operator point to the same object and `True` otherwise.
|
||||
|
||||
---
|
||||
### Time for some hash brownies!
|
||||
|
||||
```py
|
||||
@ -818,6 +835,7 @@ some_dict[5] = "Python"
|
||||
```
|
||||
* This StackOverflow [answer](https://stackoverflow.com/a/32211042/4354153) explains beautifully the rationale behind it.
|
||||
|
||||
---
|
||||
### Skipping lines?
|
||||
|
||||
```py
|
||||
@ -839,6 +857,7 @@ Some Unicode characters look identical to ASCII ones, but are considered distinc
|
||||
>>> print(value)
|
||||
```
|
||||
|
||||
---
|
||||
### Name resolution ignoring class scope
|
||||
|
||||
1\.
|
||||
@ -875,11 +894,12 @@ class SomeClass:
|
||||
5
|
||||
```
|
||||
|
||||
##### 💡 Explaination
|
||||
#### 💡 Explaination
|
||||
- Scopes nested inside class definition ignore names bound at the class level.
|
||||
- A generator expression has its own scope.
|
||||
- Starting from Python 3.X, list comprehensions also have their own scope.
|
||||
|
||||
---
|
||||
### From filled to None in one instruction...
|
||||
|
||||
```py
|
||||
@ -902,10 +922,11 @@ None
|
||||
None
|
||||
```
|
||||
|
||||
##### 💡 Explaination
|
||||
#### 💡 Explaination
|
||||
|
||||
Most methods that modiy the items of sequence/mapping objects like `list.append`, `dict.update`, `list.sort`, etc modify the objects in-place and return `None`. The rationale behind this is to improve performance by avoiding making a copy of the object if the operation can be done in-place (Referred from [here](http://docs.python.org/2/faq/design.html#why-doesn-t-list-sort-return-the-sorted-list))
|
||||
|
||||
---
|
||||
### Deleting a list item while iterating over it
|
||||
|
||||
```py
|
||||
@ -964,6 +985,7 @@ Can you guess why the output is [2, 4]?
|
||||
|
||||
* See this nice StackOverflow [thread](https://stackoverflow.com/questions/45877614/how-to-change-all-the-dictionary-keys-in-a-for-loop-with-d-items) for a similar example related to dictionaries in Python.
|
||||
|
||||
---
|
||||
### Explicit typecast of strings
|
||||
|
||||
```py
|
||||
@ -1000,6 +1022,7 @@ nan
|
||||
`'inf'` and `'nan'` are special strings (case-insensitieve), which when explicitly type casted to `float` type, are used to represent mathematical "infinity" and "not a number" respectively.
|
||||
|
||||
|
||||
---
|
||||
### Well, something is fishy...
|
||||
|
||||
```py
|
||||
@ -1032,6 +1055,7 @@ print(square(10))
|
||||
TabError: inconsistent use of tabs and spaces in indentation
|
||||
```
|
||||
|
||||
---
|
||||
### Class attributes and instance attributes
|
||||
|
||||
1\.
|
||||
@ -1101,6 +1125,7 @@ True
|
||||
* Class variables and variables in class instances are internally handled as dictionaries of a class object. If a variable name is not found in the dictionary of current class, the parent classes are searched for it.
|
||||
* The `+=` operator modifies the mutable object in-place without creating a new object. So changing the attribute of one instance, affects the other instances and the class attribute as well.
|
||||
|
||||
---
|
||||
### Catching the Exceptions!
|
||||
|
||||
```py
|
||||
@ -1174,6 +1199,7 @@ SyntaxError: invalid syntax
|
||||
list.remove(x): x not in list
|
||||
```
|
||||
|
||||
---
|
||||
### String concatenation
|
||||
|
||||
This is not a WTF at all, but just some nice things to be aware of :)
|
||||
@ -1222,6 +1248,7 @@ def convert_list_to_string(l, iters):
|
||||
- Or better, if already you've contents available in the form of an iterable object, then use `''.join(iterable_object)` which is much faster.
|
||||
|
||||
|
||||
---
|
||||
### Minor Ones
|
||||
|
||||
* `join()` is a string operation instead of list operation. (sort of counter-intuitive at first usage)
|
||||
@ -1345,6 +1372,7 @@ e
|
||||
tuple()
|
||||
```
|
||||
|
||||
---
|
||||
### ## 💡 Explaination:
|
||||
* The correct statement for expected behavior is `t = ('one',)` or `t = 'one',` (missing comma) otherwise the interpreter considers `t` to be a `str` and iterates over it character by character.
|
||||
* `()` is a special token and denotes empty `tuple`.
|
||||
|
Loading…
Reference in New Issue
Block a user