1
0
mirror of https://github.com/satwikkansal/wtfpython synced 2024-11-29 14:34:21 +01:00

Add new example: Wild imports

This commit is contained in:
Satwik Kansal 2019-06-11 00:43:32 +05:30
parent c6e0d795ff
commit 8b4bbb2196

115
README.md vendored
View File

@ -1856,21 +1856,22 @@ print(x, ': x in global')
``` ```
3\. 3\.
```
x = 1
print([x for x in range(5)])
print(x, ': x in global')
```
**Output (on Python 2.x):** **Output (Python 2.x):**
``` ```
>>> x = 1
>>> print([x for x in range(5)])
[0, 1, 2, 3, 4] [0, 1, 2, 3, 4]
>>> print(x, ': x in global')
(4, ': x in global') (4, ': x in global')
``` ```
**Output (on Python 3.x):** **Output (Python 3.x):**
``` ```
>>> x = 1
>>> print([x for x in range(5)])
[0, 1, 2, 3, 4] [0, 1, 2, 3, 4]
>>> print(x, ': x in global')
1 : x in global 1 : x in global
``` ```
@ -2183,12 +2184,10 @@ class SomeClass:
- A generator expression has its own scope. - A generator expression has its own scope.
- Starting from Python 3.X, list comprehensions also have their own scope. - Starting from Python 3.X, list comprehensions also have their own scope.
---
--- ---
### ▶ Needles in a Haystack ### ▶ Needles in a Haystack ^
1\. 1\.
```py ```py
@ -2227,7 +2226,31 @@ e
tuple() tuple()
``` ```
3\. Not asserting strongly enough 3\.
```
ten_words_list = [
"some",
"very",
"big",
"list",
"that"
"consists",
"of",
"exactly",
"ten",
"words"
]
```
**Output**
```py
>>> len(ten_words_list)
9
```
4\. Not asserting strongly enough
```py ```py
a = "python" a = "python"
@ -2244,7 +2267,12 @@ b = "javascript"
* For 1, the correct statement for expected behavior is `x, y = (0, 1) if True else (None, None)`. * For 1, the correct statement for expected behavior is `x, y = (0, 1) if True else (None, None)`.
* For 2, 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. * For 2, 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`. * `()` is a special token and denotes empty `tuple`.
* For 3, no `AssertionError` was raised because we're asserting entire tuple, instead of asserting the individual expression `a == b`. The following snippet will clear things up, * In 3, as you might have already figured out, there's a missing comma after 5th element (`"that"`) in the list. So by implicit string literal concatenation,
```py
>>> ten_words_list
['some', 'very', 'big', 'list', 'thatconsists', 'of', 'exactly', 'ten', 'words']
```
* No `AssertionError` was raised in 4th snippet because instead of asserting the individual expression `a == b`, we're asserting entire tuple. The following snippet will clear things up,
```py ```py
>>> a = "python" >>> a = "python"
>>> b = "javascript" >>> b = "javascript"
@ -2264,7 +2292,60 @@ b = "javascript"
--- ---
### ▶ Wild imports
```py
# File: module.py
def some_weird_name_func_():
print("works!")
def _another_weird_name_func():
print("works!")
```
**Output**
```py
>>> from module import *
>>> some_weird_name_func_()
"works!"
>>> _another_weird_name_func()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_another_weird_name_func' is not defined
```
#### 💡 Explanation:
- It if often adivsable to not use wildcard imports. In wildcard imports, the names with leading underscore are be imported. This may lead to errors in runtime.
- Had we used `from ... import a, b, c` syntax, the above `NameError` won't have occurred.
```py
>>> from module import some_weird_name_func_, _another_weird_name_func
>>> _another_weird_name_func()
works!
```
- If you really want to use wildcard imports, then you'd have to define the list `__all__` in your module that will contain a list of public objects that'll be available when we do wildcard imports.
```py
__all__ = ['_another_weird_name_func']
def some_weird_name_func_():
print("works!")
def _another_weird_name_func():
print("works!")
```
**Output**
```py
>>> _another_weird_name_func()
"works!"
>>> some_weird_name_func_()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'some_weird_name_func_' is not defined
```
--- ---
@ -2561,12 +2642,12 @@ The spelling is intended. Please, don't submit a patch for this.
class Yo(object): class Yo(object):
def __init__(self): def __init__(self):
self.__honey = True self.__honey = True
self.bitch = True self.bro = True
``` ```
**Output:** **Output:**
```py ```py
>>> Yo().bitch >>> Yo().bro
True True
>>> Yo().__honey >>> Yo().__honey
AttributeError: 'Yo' object has no attribute '__honey' AttributeError: 'Yo' object has no attribute '__honey'
@ -2574,7 +2655,7 @@ AttributeError: 'Yo' object has no attribute '__honey'
True True
``` ```
Why did `Yo()._Yo__honey` work? Only Indian readers would understand. Why did `Yo()._Yo__honey` work?
#### 💡 Explanation: #### 💡 Explanation:
@ -2584,6 +2665,10 @@ Why did `Yo()._Yo__honey` work? Only Indian readers would understand.
--- ---
--- ---
## Section: Miscellaneous ## Section: Miscellaneous