Python的print
声明通常似乎打印repr()
其输入.元组似乎不是例外:
>>> print (1, 2, 3)
(1, 2, 3)
>>> print repr((1, 2, 3))
(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
但是当我弄乱CPython的内部时,我偶然发现了一些奇怪的行为.简而言之:如果您使用Python 2来创建自引用元组,则直接打印它的行为与打印其repr()
/ str()
/ unicode()
表示完全不同.
>>> print outer # refer to the link above
((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
... many lines later ...
((((((((((Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MemoryError: stack overflow
>>> print repr(outer)
((...),)
>>> print str(outer)
((...),)
>>> print unicode(outer)
((...),)
Run Code Online (Sandbox Code Playgroud)
到底是print
做什么的?为了尝试自己回答这个问题,我提到了语言参考:
6.6.该
print …
我有一个在gunicorn上运行的WSGI-app(一个Django项目)127.0.0.1:18731
,我使用Apache和mod_proxy将请求重定向http://example.com/my-project/*
到http://127.0.0.1:18731/*
.静态文件存储在外部/my-project/
.如果Django应用程序不需要重定向任何东西,这可以正常工作,但如果它试图重定向请求(例如添加一个尾部斜杠http://example.com/my-project/foo
),它最终会/my-project/
从URL中删除,留下无效的URL http://example.com/foo/
.
我的mod_proxy配置如下:
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyRequests On
ProxyPass /my-project/ http://127.0.0.1:18731/ retry=0
ProxyPassReverse /my-project/ http://127.0.0.1:18731/ retry=0
ProxyPreserveHost On
ProxyErrorOverride Off
Run Code Online (Sandbox Code Playgroud)
为了/my-project/
便于携带,我不想强制Django为其所有URL 添加前缀.Apache显然应该使用该ProxyPassReverse
行自己处理前缀.我究竟做错了什么?
我有一个Python应用程序,简而言之,从远程服务器接收数据,处理它,响应服务器,偶尔将处理后的数据保存到磁盘.我遇到的问题是要写入大量数据,并且保存过程可能需要超过半分钟.这显然是一种阻塞操作,因此在此期间网络IO会停止.我希望能够在后台进行保存操作,可以这么说,以便应用程序可以合理地快速地继续与服务器通信.
我知道,我也许需要某种线程模块来实现这一点,但我不能告诉区别是什么之间thread
,threading
,multiprocessing
和各种其他选项.有人知道我在找什么吗?
根据C99规范WG14/N1124草案§6.4.2,"标识符":
Run Code Online (Sandbox Code Playgroud)identifier: identifier-nondigit identifier identifier-nondigit identifier digit identifier-nondigit: nondigit universal-character-name other implementation-defined characters
§6.4.3,"通用字符名称":
Run Code Online (Sandbox Code Playgroud)universal-character-name: \u hex-quad \U hex-quad hex-quad
这似乎表明类似的标识符foo\u00AAbar
根据语法是有效的.我在语义上找不到任何暗示的东西.然而GCC拒绝这样的标识符,甚至没有尝试解析它:
<stdin>:2: error: stray ‘\’ in program
<stdin>:2: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘u00AAbar’
<stdin>:2: error: ‘u00AAbar’ undeclared (first use in this function)
Run Code Online (Sandbox Code Playgroud)
这是对GCC,C99标准或我的推理的疏忽吗?
tokens_raw = {"foo": "bar"}
tokens_raw_old = { }
while not tokens_raw == tokens_raw_old:
tokens_raw_old = tokens_raw
# while loop that modifies tokens_raw goes here;
# tokens_raw_old is never referenced
print tokens_raw_old == tokens_raw
Run Code Online (Sandbox Code Playgroud)
由于某种原因,这在第一次之后输出True.tokens_raw_old
具有相同的数据tokens_raw
,即使在tokens_raw
单独修改后也是如此.我是否在某个地方犯了一个愚蠢的错误,或问题是否在第二个while循环中(再次,从未引用过tokens_raw_old
)?如果没有明显的错误,我会发布更多代码.
我有一个配置变量的字典,看起来像这样:
self.config = {
"foo": "abcdef",
"bar": 42,
"xyz": True
}
Run Code Online (Sandbox Code Playgroud)
我希望能够从用户输入更新这些变量(在这种情况下,它将始终以字符串的形式).我面临的问题很明显,我的第一个解决方案似乎对我来说足够好了:
def updateconfig(self, key, value):
if key in self.config:
self.config[key] = type(self.config[key])(value)
Run Code Online (Sandbox Code Playgroud)
然而,Freenode中的#python几乎似乎被冒犯了,我建议这样的解决方案.有人能告诉我为什么这是不好的做法?
python ×4
apache ×1
blocking ×1
c ×1
c99 ×1
cpython ×1
django ×1
equality ×1
gunicorn ×1
io ×1
nonblocking ×1
python-2.7 ×1
types ×1
user-input ×1
while-loop ×1
wsgi ×1