Code Monkey home page Code Monkey logo

wtfpython-cn's Issues

MacOS pip3.6 安装wtfpython, run wtfpython后报错FileNotFoundError

➜ tutorial git:(release) ✗ pip install wtfpython -U
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting wtfpython
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/3d/9b/3ca0faf98e3ebb1bfce399579e6f59eee3efbd37d9cf3918315193cb5e2c/wtfpython-0.2-py2.py3-none-any.whl
Installing collected packages: wtfpython
Successfully installed wtfpython-0.2

➜ tutorial git:(release) ✗ wtfpython
Fetching the latest version...
<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:847)>
Uh oh, failed to check for the latest version, using the local version for now.
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/bin/wtfpython", line 10, in
sys.exit(load_and_read())
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/wtf_python/main.py", line 31, in load_and_read
render_doc()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/wtf_python/main.py", line 24, in render_doc
with open(file_name, 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'content.md'

Strings can be tricky sometimes/微妙的字符串 *中的疑问

疑问1:所有长度为 0 和长度为 1 的字符串都被驻留.
以下是我测试的代码

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 'ka'
>>> len(a)
2
>>> id(a)
1439701558472
>>> len('ka')
2
>>> id('ka')
1439701558472

长度为2的字符串也驻留了吗?

Section: Strain your brain! --> Strings can be tricky sometimes *中有条示例似乎已经不适用了

首先,感谢作者的辛勤劳作,实践中发现Strings can be tricky sometimes/微妙的字符串 *中第2段代码段最后一条示例在我的环境中的运行结果与文中恰恰相反,原文中是这样的:

>>> a, b = "wtf!", "wtf!"
>>> a is b
True

我的环境中是这样的:

$ python3
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a, b = "wtf!", "wtf!"
>>> a is b
False

我的操作环境:windows 10企业版(64位操作系统,基于x64处理器),Python 3.7.1(64位)。
请查证。

False = True!

True  =  False 
if  True  ==  False :
     print ( " I've lost faith in truth! " )
True = False
  File "<ipython-input-12-6fa9faf6b782>", line 1
    True = False
                ^
SyntaxError: can't assign to keyword

Python 3.6.6

『Strings can be tricky sometimes/微妙的字符串 *』中 问题

作者:您好,我发现『Strings can be tricky sometimes/微妙的字符串 *』中
问题1:

>>> 'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa'
False

而我运行结果:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa'
True

与您的结果相反,请问为什么呢?难道是“只有长度小于 20 的字符串才会发生常量折叠”结论不适用了么?
问题2:
博客中最后的代码:

>>> a,b = 400,400
>>> id(a) == id(b)
True

而我允许结果为:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a, b = 400, 400
>>> id(a) == id(b)
False

结果也是相反的。

Subclass relationships/子类关系 * 的翻译

原话:
Python 中的子类关系并不必须是传递的. 任何人都可以在元类中随意定义 subclasscheck.
当 issubclass(cls, Hashable) 被调用时, 它只是在 cls 中寻找 "hash" 方法或继承自"hash"的方法.
修改:
Python 中的子类关系并不必须是传递的---->Python 中的子类关系不一定是传递的
它只是在 cls 中寻找 "hash" 方法或继承自"hash"的方法. -----> 它只是在 cls 中寻找 "hash" 方法或者去继承的父类中寻找"hash"的方法.

Section: Strings can be tricky sometimes/微妙的字符串 * 中常量折叠在3.7中已经迁移至新的AST 优化器

您好,在拜读wtfpython的时候,发现在3.7的版本中有一些变化。其中,

'a' * 20 is 'aaaaaaaaaaaaaaaaaaaa'
True
'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa'
False

我的环境中是这样的:

Python 3.7.1 (default, Dec 14 2018, 13:28:58)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.

'a' * 20 is 'aaaaaaaaaaaaaaaaaaaa'
True
'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa'
True

查了下相关资料,在3.7版本中,常量折叠已经从窥孔优化器迁移至新的 AST 优化器,后者可以以更高的一致性来执行优化。 (由 Eugene Toder 和 INADA Naoki 在 bpo-29469bpo-11549 中贡献。)

Beware of default mutable arguments!/当心默认的可变参数!

Python中函数的默认可变参数并不是每次调用该函数时都会被初始化. 相反, 它们会使用最近分配的值作为默认值.什么时候会分配值

>>> def some_func(default_arg1=[], default_arg2=[]):
...     default_arg1.append('start')
...     default_arg2.append('end')
...     default_arg1.append(default_arg2)
...     return default_arg1, default_arg2
>>> some_func.__defaults__
([], [])
>>> some_func()
(['start', ['end']], ['end'])
>>> some_func.__defaults__
(['start', ['end']], ['end'])
>>> some_func([]) 
(['start', ['end', 'end']], ['end', 'end'])
>>> some_func.__defaults__
(['start', ['end', 'end']], ['end', 'end'])
>>> some_func([])
(['start', ['end', 'end', 'end']], ['end', 'end', 'end'])
>>> some_func.__defaults__
(['start', ['end', 'end', 'end']], ['end', 'end', 'end'])
>>> some_func([],[])
(['start', ['end']], ['end'])
>>> some_func.__defaults__  #默认值为什么没有重新分配???
(['start', ['end', 'end', 'end']], ['end', 'end', 'end'])
>>> some_func()
(['start', ['end', 'end', 'end', 'end'], 'start', ['end', 'end', 'end', 'end']], ['end', 'end', 'end', 'end'])
>>> some_func.__defaults__
(['start', ['end', 'end', 'end', 'end'], 'start', ['end', 'end', 'end', 'end']], ['end', 'end', 'end', 'end'])

对/=运算符的疑惑

您好, 不知道在这里问问题是否合适. 以下代码的结果有点让人疑惑:

import numpy as np
a2 = 4*np.ones((256,256))+1j*2*np.ones((256,256))
b2 = 2*np.ones((256,256))
a2 /= np.abs(a2)*b2

a3 = 4*np.ones((256,256))+1j*2*np.ones((256,256))
b3 = 2*np.ones((256,256))
a3 = a3/np.abs(a3)*b3

np.allclose(a2,a3)

a3结果正确, a2错误. 为什么a2与a3不相等呢?

windows 10 运行 wtfpython 报错

电脑上安装的python 版本是:3.6.7
已经通过 pip 安装好了 wtfpython
但是运行时报错 "GBK" 编码错误
Traceback (most recent call last): File "c:\python3\lib\runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "c:\python3\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\python3\Scripts\wtfpython.exe\__main__.py", line 9, in <module> File "c:\python3\lib\site-packages\wtf_python\main.py", line 31, in load_and_read render_doc() File "c:\python3\lib\site-packages\wtf_python\main.py", line 25, in render_doc content = f.read() UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 284: illegal multibyte sequence

一个未配对的括号

字符串在编译时被实现 ('wtf' 将被驻留, 但是 ''.join(['w', 't', 'f'] 将不会被驻留)

is is not what it is!/出人意料的is! 单行赋值,交互式环境执行的版本问题

文中说到当 a 和 b 在同一行中被设置为 257 时, Python 解释器会创建一个新对象, 然后同时引用第二个变量. 如果你在不同的行上进行, 它就不会 "知道" 已经存在一个 257 对象了.并且这是特别为交互式环境做的编译器优化,直接执行文件不会有此现象发生。

但是经过在我电脑测试后发现:

python 3.7.3的ipython交互式环境中:
同一行的代码a = 257; b = 257a, b = 257, 257执行a is b的结果:两者均为False
但是文件执行结果:前者False,后者为True

python 3.6.8的ipython交互式环境中:
同一行的代码a = 257; b = 257a, b = 257, 257执行a is b的结果:前者为False,后者为True
但是文件执行结果:两者均为True

详细结果如下:

python版本3.7.3

ipython交互式环境运行:

Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: a = 257; b = 257

In [2]: a is b
Out[2]: False

In [3]: a, b = 257, 257

In [4]: a is b
Out[4]: False

控制台命令 python 文件名 运行:

a, b = 257, 257
print(a is b)   # False

a = 257; b = 257
print(a is b)   # True

python版本3.6.8

ipython交互式环境运行:

Python 3.6.8 (default, May  2 2019, 20:40:44) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: a = 257; b = 257

In [2]: a is b
Out[2]: False

In [3]: a, b = 257, 257

In [4]: a is b
Out[4]: True

控制台命令 python 文件名 运行:

a, b = 257, 257
print(a is b)   # True

a = 257; b = 257
print(a is b)   # True

About Evaluation time discrepancy/执行时机差异

>>> array=[1,8,15]
>>> g=(x for x in array if array.count(x)>0)
>>> array=[2,8,22]
>>> list(g)
[8]
>>> array=[1,8,15]
>>> g=(x for x in array if array.count(x)>0)
>>> list(g)
[1, 8, 15]
>>> array=[2,8,22]
>>> list(g)
[]

Why?

中文版本的更新问题

当前中文版本对应于英文版的30e05a5
英文版在这之后有一些更新。我准备与大家一起将中文版更新至英文版cd4d7c0
此次更新计划实施6个月,首先,增加新案例;其次,将原案例更新部分添加进来;最后,针对目录及案例的顺序进行调整。
暂时就想到这些,第一次准备进行项目pr,大家一起来讨论啊

Evaluation time discrepancy 评估时间差异->执行时机差异

这一小结主要讲的内容是生成器表达式中,in字句和条件语句会在不同的时间段被执行。原标题“评估时间差异”在没有看下文或者英文原文的情况下容易被理解为“评估时间长短上的区别”,可能会有一点费解。改为“执行时机差异”,会不会更容易理解?

ipython 运行代码

‘出人意料的is’, 一节中 a = 257; b = 256, 在ipython中会出现不同的输出
a = 257; b = 257
In [2]: a is b
Out[2]: False

一些建议和补充

首先,感谢大大提供的翻译!
上周末大致读完后,以下几个地方想做一些补充

  1. 项目中多次出现的change in place翻译为“原地更新”,个人觉得翻译为“更新对象本身”更容易理解。例如 Mutating the immutable!/强人所难中的这一句:
    += 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.
    原翻译为“+= 操作符在原地修改了列表. 元素赋值操作并不工作, 但是当异常抛出时, 元素已经在原地被修改了”
    可修改为“+= 操作符修改了列表本身. 元素赋值操作并不能正常运行, 但是当异常抛出时, 元素本身已经被修改了”
  2. Let's see if you can guess this?/看看你能否猜到这一点这一节的说明中第三点和第六点中的“元祖”应为“元组”
  3. Inpinity/无限这里的拼写应该是为了突出python中无限的定义与pi有关,因此将两个单词拼在一起
  4. Mangling time!/修饰时间!这一小节中印度人的梗可能与Yo Yo Honey Singh这位印度音乐人有关
  5. 循环变量泄漏这一节的说明这里提到的python3中列表推导的更新,可以参考这个stackoverflow上的讨论 个人的理解是list comprehension和list(generator expression)两者在实现上并不一样,但在语法和特性上可以参考着来看

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.