我在Python解释器中运行以下代码:
>>> foo = 10
>>> dir(foo) == dir(10)
True
>>> dir(foo) is dir(10)
False
>>>
Run Code Online (Sandbox Code Playgroud)
为什么是这样?
似乎有一些一致性,因为调用set()
字符串似乎总是解析为相同的(非alabetical)顺序,并且两者都是
set([1,2,3]) & set([1,2,3,4])
Run Code Online (Sandbox Code Playgroud)
和它的表兄弟混在一起
set([2,3,1]) & set([4,3,1,2])
Run Code Online (Sandbox Code Playgroud)
将导致有序的set([1,2,3])
.
另一方面,有点像racy,比如
from random import randint
set([randint(0,9) for x in range(3)])
Run Code Online (Sandbox Code Playgroud)
有时会给set([9, 6, 7])
...
... 这里发生了什么?
我正在尝试使用该pprint
模块检查Python中的一些变量,我可以愉快地使用交互式shell和下面的代码:
import pprint
pp = pprint.PrettyPrinter()
stuff = ['cakes','bread','mead']
pp.pprint(stuff)
Run Code Online (Sandbox Code Playgroud)
但是,当我把上面的内容放入pprint.py
并使用它运行时python pprint.py
我得到错误:
$ python dev/pars/pprint.py
Traceback (most recent call last):
File "dev/pars/pprint.py", line 1, in ?
import pprint
File "/home/origina2/dev/pars/pprint.py", line 2, in ?
pp = pprint.PrettyPrinter()
AttributeError: 'module' object has no attribute 'PrettyPrinter'
Run Code Online (Sandbox Code Playgroud)
与交互式shell相比,从文件运行Python代码时调用模块的方式有何不同?