Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9323b86

Browse files
authored
Merge pull request#380 from nifadyev/fix/#369/fix-markdownlint-errors-1
#379: Fix markdownlint errors (part 1)
2 parents482f230 +a6a6473 commit9323b86

File tree

3 files changed

+87
-39
lines changed

3 files changed

+87
-39
lines changed

‎.github/workflows/pr.yml‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ jobs:
1717
-name:Write git diff to temp file
1818
run:|
1919
git fetch origin
20-
git diff origin/${{ github.base_ref }} --unified=0 *.md translations/*/*.md \
20+
git diff origin/${{ github.base_ref }} *.md translations/*/*.md \
21+
| sed -n '/^+/p' | sed '/^+++/d' | sed 's/^+//' \
2122
> ${{ runner.temp }}/diff.md
22-
-uses:DavidAnson/markdownlint-cli2-action@v17
23+
-name:Output diff
24+
run:cat ${{ runner.temp }}/diff.md
25+
-name:Check diff with markdownlint
26+
uses:DavidAnson/markdownlint-cli2-action@v17
2327
with:
2428
globs:"${{ runner.temp }}/diff.md"

‎.pre-commit-config.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ default_language_version:
22
python:python3.12
33
repos:
44
-repo:https://github.com/DavidAnson/markdownlint-cli2
5-
rev:v0.14.0
5+
rev:v0.17.0
66
hooks:
77
-id:markdownlint-cli2

‎README.md‎

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,33 @@
99
<h1align="center">What the f*ck Python! 😱</h1>
1010
<palign="center">Exploring and understanding Python through surprising snippets.</p>
1111

12-
Translations:[Chinese 中文](https://github.com/leisurelicht/wtfpython-cn) |[Vietnamese Tiếng Việt](https://github.com/vuduclyunitn/wtfptyhon-vi) |[Spanish Español](https://web.archive.org/web/20220511161045/https://github.com/JoseDeFreitas/wtfpython-es) |[Korean 한국어](https://github.com/buttercrab/wtfpython-ko) |[Russian Русский](https://github.com/satwikkansal/wtfpython/tree/master/translations/ru-russian) |[German Deutsch](https://github.com/BenSt099/wtfpython) |[Persian فارسی](https://github.com/satwikkansal/wtfpython/tree/master/translations/fa-farsi) |[Add translation](https://github.com/satwikkansal/wtfpython/issues/new?title=Add%20translation%20for%20[LANGUAGE]&body=Expected%20time%20to%20finish:%20[X]%20weeks.%20I%27ll%20start%20working%20on%20it%20from%20[Y].)
12+
Translations:[Chinese 中文](https://github.com/leisurelicht/wtfpython-cn) |
13+
[Vietnamese Tiếng Việt](https://github.com/vuduclyunitn/wtfptyhon-vi) |
14+
[Spanish Español](https://web.archive.org/web/20220511161045/https://github.com/JoseDeFreitas/wtfpython-es) |
15+
[Korean 한국어](https://github.com/buttercrab/wtfpython-ko) |
16+
[Russian Русский](https://github.com/satwikkansal/wtfpython/tree/master/translations/ru-russian) |
17+
[German Deutsch](https://github.com/BenSt099/wtfpython) |
18+
[Persian فارسی](https://github.com/satwikkansal/wtfpython/tree/master/translations/fa-farsi) |
19+
[Add translation](https://github.com/satwikkansal/wtfpython/issues/new?title=Add%20translation%20for%20[LANGUAGE]&body=Expected%20time%20to%20finish:%20[X]%20weeks.%20I%27ll%20start%20working%20on%20it%20from%20[Y].)
1320

1421
Other modes:[Interactive Website](https://wtfpython-interactive.vercel.app) |[Interactive Notebook](https://colab.research.google.com/github/satwikkansal/wtfpython/blob/master/irrelevant/wtf.ipynb)
1522

16-
Python, being a beautifully designed high-level and interpreter-based programming language, provides us with many features for the programmer's comfort. But sometimes, the outcomes of a Python snippet may not seem obvious at first sight.
23+
Python, being a beautifully designed high-level and interpreter-based programming language,
24+
provides us with many features for the programmer's comfort.
25+
But sometimes, the outcomes of a Python snippet may not seem obvious at first sight.
1726

18-
Here's a fun project attempting to explain what exactly is happening under the hood for some counter-intuitive snippets and lesser-known features in Python.
27+
Here's a fun project attempting to explain what exactly is happening under the hood for some counter-intuitive snippets
28+
and lesser-known features in Python.
1929

20-
While some of the examples you see below may not be WTFs in the truest sense, but they'll reveal some of the interesting parts of Python that you might be unaware of. I find it a nice way to learn the internals of a programming language, and I believe that you'll find it interesting too!
30+
While some of the examples you see below may not be WTFs in the truest sense,
31+
but they'll reveal some of the interesting parts of Python that you might be unaware of.
32+
I find it a nice way to learn the internals of a programming language, and I believe that you'll find it interesting too!
2133

22-
If you're an experienced Python programmer, you can take it as a challenge to get most of them right in the first attempt. You may have already experienced some of them before, and I might be able to revive sweet old memories of yours!:sweat_smile:
34+
If you're an experienced Python programmer, you can take it as a challenge to get most of them right in the first attempt
35+
You may have already experienced some of them before, and I might be able to revive sweet old memories of yours!:sweat_smile:
2336

24-
PS: If you're a returning reader, you can learn about the new modifications[here](https://github.com/satwikkansal/wtfpython/releases/) (the examples marked with asterisk are the ones added in the latest major revision).
37+
PS: If you're a returning reader, you can learn about the new modifications[here](https://github.com/satwikkansal/wtfpython/releases/)
38+
(the examples marked with asterisk are the ones added in the latest major revision).
2539

2640
So, here we go...
2741

@@ -116,6 +130,8 @@ So, here we go...
116130

117131
All the examples are structured like below:
118132

133+
>##Section: (if necessary)
134+
>
119135
>###▶ Some fancy Title
120136
>
121137
>```py
@@ -148,17 +164,20 @@ All the examples are structured like below:
148164
># some justified output
149165
>```
150166
151-
**Note:** All the examples are tested on Python3.5.2 interactive interpreter,and they should workforall the Python versions unless explicitly specified before the output.
167+
**Note:** All the examples are tested on Python3.5.2 interactive interpreter,
168+
and they should workforall the Python versions unless explicitly specified before the output.
152169
153170
# Usage
154171
155172
A nice way to get the most out of these examples,in my opinion,is to read themin sequential order,andfor every example:
156173
157-
- Carefully read the initial codefor setting up the example. If you're an experienced Python programmer, you'll successfully anticipate what's going to happen next most of the time.
174+
- Carefully read the initial codefor setting up the example.
175+
If you're an experienced Python programmer, you'll successfully anticipate what's going to happen next most of the time.
158176
- Read the output snippetsand,
159177
- Checkif the outputs are the sameas you'd expect.
160178
- Make sureif you know the exact reason behind the output being the way itis.
161-
- If the answeris no (whichis perfectly okay), take a deep breath,and read the explanation (andif you still don't understand, shout out! and create an issue [here](https://github.com/satwikkansal/wtfpython/issues/new)).
179+
- If the answeris no (whichis perfectly okay), take a deep breath,and read the explanation
180+
(andif you still don't understand, shout out! and create an issue [here](https://github.com/satwikkansal/wtfpython/issues/new)).
162181
- If yes, give a gentle pat on your back,and you may skip to thenext example.
163182
164183
---
@@ -170,7 +189,6 @@ A nice way to get the most out of these examples, in my opinion, is to read them
170189
### ▶ First things first! \*
171190
172191
<!-- ExampleID: d3d73936-3cf1-4632-b5ab-817981338863-->
173-
<!-- read-only-->
174192
175193
For some reason, the Python3.8's "Walrus" operator (`:=`) has become quite popular. Let's check it out,
176194
@@ -230,9 +248,8 @@ SyntaxError: invalid syntax
230248
231249
#### 💡 Explanation
232250
233-
**Quick walrus operator refresher**
234-
235-
The Walrus operator (`:=`) was introducedin Python3.8, it can be usefulin situations where you'd want to assign values to variables within an expression.
251+
The Walrus operator (`:=`) was introducedin Python3.8,
252+
it can be usefulin situations where you'd want to assign values to variables within an expression.
236253
237254
```py
238255
def some_func():
@@ -264,12 +281,13 @@ if a := some_func():
264281
265282
This saved one line of code,and implicitly prevented invoking`some_func` twice.
266283
267-
- Unparenthesized"assignment expression" (use of walrus operator),is restricted at the top level, hence the`SyntaxError`in the`a:="wtf_walrus"` statement of the first snippet. Parenthesizing it workedas expectedand assigned`a`.
268-
269-
- As usual, parenthesizing of an expression containing`=` operatorisnot allowed. Hence the syntax errorin`(a, b =6,9)`.
270-
271-
- The syntax of the Walrus operatoris of the form`NAME:= expr`, where`NAME`is a valid identifier,and`expr`is a valid expression. Hence, iterable packingand unpacking arenot supported which means,
272-
284+
- Unparenthesized"assignment expression" (use of walrus operator),is restricted at the top level,
285+
hence the`SyntaxError`in the`a:="wtf_walrus"` statement of the first snippet.
286+
Parenthesizing it workedas expectedand assigned`a`.
287+
- As usual, parenthesizing of an expression containing`=` operatorisnot allowed.
288+
Hence the syntax errorin`(a, b =6,9)`.
289+
- The syntax of the Walrus operatoris of the form`NAME:= expr`, where`NAME`is a valid identifier,
290+
and`expr`is a valid expression. Hence, iterable packingand unpacking arenot supported which means,
273291
-`(a:=6,9)`is equivalent to`((a:=6),9)`and ultimately`(a,9)` (where`a`'s value is 6')
274292
275293
```py
@@ -319,7 +337,7 @@ a = "wtf!"; b = "wtf!"
319337
assert ais b
320338
```
321339
322-
4\. **Disclaimer - snippet is notrelavant in modern Python versions**
340+
4\. **Disclaimer - snippet is notrelevant in modern Python versions**
323341
324342
**Output (< Python3.7 )**
325343
@@ -334,12 +352,15 @@ Makes sense, right?
334352
335353
#### 💡 Explanation:
336354
337-
- The behaviorin firstand second snippetsis due to a CPython optimization (called string interning) that tries to use existing immutable objectsin some cases rather than creating a newobject every time.
355+
- The behaviorin firstand second snippetsis due to a CPython optimization (called string interning)
356+
that tries to use existing immutable objectsin some cases rather than creating a newobject every time.
338357
- After being"interned," many variables may reference the same stringobjectin memory (saving memory thereby).
339-
- In the snippets above, strings are implicitly interned. The decision of when to implicitlyintern a stringis implementation-dependent. There are some rules that can be used to guessif a string will be internedornot:
358+
- In the snippets above, strings are implicitly interned. The decision of when to implicitlyintern a stringis
359+
implementation-dependent. There are some rules that can be used to guessif a string will be internedornot:
340360
- All length0and length1 strings are interned.
341361
- Strings are interned atcompile time (`'wtf'` will be interned but`''.join(['w','t','f'])` willnot be interned)
342-
- Strings that arenot composed ofASCII letters, digitsor underscores, arenot interned. This explains why`'wtf!'` wasnot interned due to`!`. CPython implementation of this rule can be found [here](https://github.com/python/cpython/blob/3.6/Objects/codeobject.c#L19)
362+
- Strings that arenot composed ofASCII letters, digitsor underscores, arenot interned.
363+
This explains why`'wtf!'` wasnot interned due to`!`. CPython implementation of this rule can be found [here](https://github.com/python/cpython/blob/3.6/Objects/codeobject.c#L19)
343364
344365
<palign="center">
345366
<picture>
@@ -349,10 +370,25 @@ Makes sense, right?
349370
</picture>
350371
</p>
351372
352-
- When`a`and`b` areset to`"wtf!"`in the same line, the Python interpreter creates a newobject, then references the second variable at the same time. If you do it on separate lines, it doesn't "know" that there's already`"wtf!"`as anobject (because`"wtf!"`isnot implicitly internedas per the facts mentioned above). It's a compile-time optimization. This optimization doesn't apply to3.7.x versions of CPython (check this [issue](https://github.com/satwikkansal/wtfpython/issues/100)for more discussion).
353-
- Acompile unitin an interactive environment like IPython consists of a single statement, whereas it consists of the entire modulein case of modules.`a, b ="wtf!","wtf!"`is single statement, whereas`a ="wtf!"; b ="wtf!"` are two statementsin a single line. This explains why the identities are differentin`a ="wtf!"; b ="wtf!"`,and also explain why they are same when invokedin`some_file.py`
354-
- The abrupt changein the output of the fourth snippetis due to a [peephole optimization](https://en.wikipedia.org/wiki/Peephole_optimization) technique knownas Constant folding. This means the expression`'a'*20`is replaced by`'aaaaaaaaaaaaaaaaaaaa'` during compilation to save a few clock cycles during runtime. Constant folding only occursfor strings having a length of less than21. (Why? Imagine the size of`.pyc`file generatedas a result of the expression`'a'*10**10`). [Here's](https://github.com/python/cpython/blob/3.6/Python/peephole.c#L288) the implementation source for the same.
355-
- Note: In Python3.7, Constant folding was moved outfrom peephole optimizer to the newAST optimizerwith some changein logicas well, so the fourth snippet doesn't work for Python 3.7. You can read more about the change [here](https://bugs.python.org/issue11549).
373+
- When`a`and`b` areset to`"wtf!"`in the same line, the Python interpreter creates a newobject,
374+
then references the second variable at the same time. If you do it on separate lines, it doesn't "know" that
375+
there's already `"wtf!"` as an object (because `"wtf!"` is not implicitly interned as per the facts mentioned above).
376+
It's a compile-time optimization. This optimization doesn't apply to3.7.x versions of CPython
377+
(check this [issue](https://github.com/satwikkansal/wtfpython/issues/100)for more discussion).
378+
- Acompile unitin an interactive environment like IPython consists of a single statement,
379+
whereas it consists of the entire modulein case of modules.`a, b ="wtf!","wtf!"`is single statement,
380+
whereas`a ="wtf!"; b ="wtf!"` are two statementsin a single line.
381+
This explains why the identities are differentin`a ="wtf!"; b ="wtf!"`,
382+
and also explain why they are same when invokedin`some_file.py`
383+
- The abrupt changein the output of the fourth snippetis due to a
384+
[peephole optimization](https://en.wikipedia.org/wiki/Peephole_optimization) technique knownas Constant folding.
385+
This means the expression`'a'*20`is replaced by`'aaaaaaaaaaaaaaaaaaaa'` during compilation to save
386+
a few clock cycles during runtime. Constant folding only occursfor strings having a length of less than21.
387+
(Why? Imagine the size of`.pyc`file generatedas a result of the expression`'a'*10**10`).
388+
[Here's](https://github.com/python/cpython/blob/3.6/Python/peephole.c#L288) the implementation source for the same.
389+
- Note: In Python3.7, Constant folding was moved outfrom peephole optimizer to the newAST optimizer
390+
with some changein logicas well, so the fourth snippet doesn't work for Python 3.7.
391+
You can read more about the change [here](https://bugs.python.org/issue11549).
356392
357393
---
358394
@@ -385,19 +421,23 @@ False
385421
386422
As per https://docs.python.org/3/reference/expressions.html#comparisons
387423
388-
> Formally,if a, b, c,..., y, z are expressionsand op1, op2,..., opN are comparison operators, then a op1 b op2 c... y opN zis equivalent to a op1 band b op2 cand... y opN z,except that each expressionis evaluated at most once.
424+
> Formally,if a, b, c,..., y, z are expressionsand op1, op2,..., opN are comparison operators,
425+
then a op1 b op2 c... y opN zis equivalent to a op1 band b op2 cand... y opN z,
426+
except that each expressionis evaluated at most once.
389427
390-
While such behavior might seem silly to youin the above examples, it's fantastic with stuff like `a == b == c` and `0 <= x <= 100`.
428+
While such behavior might seem silly to youin the above examples,
429+
it's fantastic with stuff like `a == b == c` and `0 <= x <= 100`.
391430
392431
-`FalseisFalseisFalse`is equivalent to`(FalseisFalse)and (FalseisFalse)`
393-
-`TrueisFalse==False`is equivalent to`(TrueisFalse)and (False==False)`and since the first part of the statement (`TrueisFalse`) evaluates to`False`, the overall expression evaluates to`False`.
432+
-`TrueisFalse==False`is equivalent to`(TrueisFalse)and (False==False)`
433+
and since the first part of the statement (`TrueisFalse`) evaluates to`False`, the overall expression evaluates to`False`.
394434
-`1>0<1`is equivalent to`(1>0)and (0<1)` which evaluates to`True`.
395435
- The expression`(1>0)<1`is equivalent to`True<1`and
396436
397437
```py
398438
>>>int(True)
399439
1
400-
>>>True+1#not relevant for this example, but just for fun
440+
>>>True+1#not relevant for this example, but just for fun
401441
2
402442
```
403443
@@ -1079,7 +1119,11 @@ The values of `x` were different in every iteration prior to appending `some_fun
10791119
10801120
#### 💡 Explanation:
10811121
1082-
- When defining a function inside a loop that uses the loop variablein its body, the loop function's closure is bound to the _variable_, not its _value_. The function looks up `x` in the surrounding context, rather than using the value of `x` at the time the function is created. So all of the functions use the latest value assigned to the variable for computation. We can see that it's using the`x`from the surrounding context (i.e. _not_ a local variable)with:
1122+
- When defining a function inside a loop that uses the loop variablein its body,
1123+
the loop function's closure is bound to the _variable_, not its _value_.
1124+
The function looks up`x`in the surrounding context, rather than using the value of`x` at the time
1125+
the functionis created. Soall of the functions use the latest value assigned to the variablefor computation.
1126+
We can see that it's using the `x` from the surrounding context (i.e. _not_ a local variable) with:
10831127
10841128
```py
10851129
>>>import inspect
@@ -1313,10 +1357,10 @@ Accessing `classm` or `method` twice, creates equal but not _same_ objects for t
13131357
```
13141358
13151359
- Having to create new"method" objects every time Python calls instance methodsand having to modify the arguments
1316-
every timein order to insert`self` affected performance badly.
1317-
CPython3.7 [solved it](https://bugs.python.org/issue26110) by introducing new opcodes that dealwith calling methods
1318-
without creating the temporary method objects. Thisis used only when the accessed functionis actually called, so the
1319-
snippets here arenot affected,and still generate methods :)
1360+
every timein order to insert`self` affected performance badly.
1361+
CPython3.7 [solved it](https://bugs.python.org/issue26110) by introducing new opcodes that dealwith calling methods
1362+
without creating the temporary method objects. Thisis used only when the accessed functionis actually called, so the
1363+
snippets here arenot affected,and still generate methods :)
13201364
13211365
### ▶ All-true-ation \*
13221366

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp