Skip to content

Commit 45c3d15

Browse files
author
wklken
committed
处理冲突
2 parents 7036032 + 1abe710 commit 45c3d15

13 files changed

+2099
-46
lines changed

README.md

+70-40
Original file line numberDiff line numberDiff line change
@@ -54,47 +54,9 @@ stackoverflow上Python相关回答整理翻译(相对来说都比较简单/散
5454

5555
* [其他](https://github.com/wklken/stackoverflow-py-top-qa/blob/master/contents/qa-others.md)
5656

57-
### 待翻译问题链接(还剩35个问题)
58-
59-
http://stackoverflow.com/questions/36932/how-can-i-represent-an-enum-in-python
60-
http://stackoverflow.com/questions/682504/what-is-a-clean-pythonic-way-to-have-multiple-constructors-in-python
61-
http://stackoverflow.com/questions/509211/pythons-slice-notation
62-
http://stackoverflow.com/questions/15376509/when-is-i-x-different-from-i-i-x-in-python
63-
http://stackoverflow.com/questions/132058/showing-the-stack-trace-from-a-running-python-application
64-
http://stackoverflow.com/questions/419163/what-does-if-name-main-do
65-
http://stackoverflow.com/questions/279237/import-a-module-from-a-relative-path
66-
http://stackoverflow.com/questions/4324558/whats-the-proper-way-to-install-pip-virtualenv-and-distribute-for-python
67-
http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers
68-
http://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points
69-
http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python
70-
http://stackoverflow.com/questions/432842/how-do-you-get-the-logical-xor-of-two-variables-in-python
71-
http://stackoverflow.com/questions/626759/whats-the-difference-between-list-and-tuples
72-
http://stackoverflow.com/questions/682367/good-python-modules-for-fuzzy-string-comparison
73-
http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python
74-
http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters
75-
http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format
76-
http://stackoverflow.com/questions/261638/how-do-i-protect-python-code
77-
http://stackoverflow.com/questions/944592/best-practice-for-python-assert
78-
http://stackoverflow.com/questions/11241523/why-does-python-code-run-faster-in-a-function
79-
http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python
80-
http://stackoverflow.com/questions/42950/get-last-day-of-the-month-in-python
81-
http://stackoverflow.com/questions/6618002/python-property-versus-getters-and-setters
82-
http://stackoverflow.com/questions/904036/chain-calling-parent-constructors-in-python
83-
http://stackoverflow.com/questions/17160162/what-is-in-python-2-7
84-
http://stackoverflow.com/questions/191673/preferred-python-unit-testing-framework
85-
http://stackoverflow.com/questions/61151/where-do-the-python-unit-tests-go
86-
http://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python
87-
http://stackoverflow.com/questions/6344076/differences-between-distribute-distutils-setuptools-and-distutils2
88-
http://stackoverflow.com/questions/1549801/differences-between-isinstance-and-type-in-python
89-
http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python
90-
http://stackoverflow.com/questions/1485841/behaviour-of-increment-and-decrement-operators-in-python
91-
http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python/6581949#6581949
92-
http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference
93-
http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument
94-
http://stackoverflow.com/questions/2970608/what-are-named-tuples-in-python
95-
http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python
96-
http://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object
57+
### 待翻译问题链接(还剩0问题)
9758

59+
qa-tuple.md:问题 (http://stackoverflow.com/questions/2970608/what-are-named-tuples-in-python)
9860

9961
### 已翻译问题链接(去重)
10062

@@ -117,6 +79,8 @@ qa-built-in.md:问题 (http://stackoverflow.com/questions/13857/can-you-explain-
11779

11880
qa-built-in.md:问题 (http://stackoverflow.com/questions/890128/python-lambda-why)
11981

82+
qa-built-in.md:问题 (http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python)
83+
12084
qa-control-flow.md:问题 (http://stackoverflow.com/questions/73663/terminating-a-python-script)
12185

12286
qa-control-flow.md:问题 (http://stackoverflow.com/questions/26595/is-there-any-difference-between-foo-is-none-and-foo-none)
@@ -141,6 +105,8 @@ qa-control-flow.md:问题 (http://stackoverflow.com/questions/743164/do-while-lo
141105

142106
qa-control-flow.md:问题 (http://stackoverflow.com/questions/135041/should-you-always-favor-xrange-over-range)
143107

108+
qa-control-flow.md:问题 (http://stackoverflow.com/questions/15376509/when-is-i-x-different-from-i-i-x-in-python)
109+
144110
qa-datetime.md:问题 (http://stackoverflow.com/questions/1697815/how-do-you-convert-a-python-time-struct-time-object-into-a-datetime-object)
145111

146112
qa-datetime.md:问题 (http://stackoverflow.com/questions/415511/how-to-get-current-time-in-python)
@@ -179,6 +145,8 @@ qa-exception.md:问题 (http://stackoverflow.com/questions/944592/best-practice-
179145

180146
qa-exception.md:问题 (http://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python)
181147

148+
qa-exception.md:问题 (http://stackoverflow.com/questions/944592/best-practice-for-python-assert)
149+
182150
qa-file.md:问题 (http://stackoverflow.com/questions/82831/how-do-i-check-if-a-file-exists-using-python)
183151

184152
qa-file.md:问题 (http://stackoverflow.com/questions/273192/python-best-way-to-create-directory-if-it-doesnt-exist-for-file-write)
@@ -219,6 +187,14 @@ qa-func.md:问题 (http://stackoverflow.com/questions/1098549/proper-way-to-use-
219187

220188
qa-func.md:问题 (http://stackoverflow.com/questions/3394835/args-and-kwargs)
221189

190+
qa-func.md:问题 (http://stackoverflow.com/questions/682504/what-is-a-clean-pythonic-way-to-have-multiple-constructors-in-python)
191+
192+
qa-func.md:问题 (http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters)
193+
194+
qa-func.md:问题 (http://stackoverflow.com/questions/11241523/why-does-python-code-run-faster-in-a-function)
195+
196+
qa-func.md:问题 (http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference)
197+
222198
qa-list.md:问题 (http://stackoverflow.com/questions/509211/the-python-slice-notation)
223199

224200
qa-list.md:问题 (http://stackoverflow.com/questions/53513/python-what-is-the-best-way-to-check-if-a-list-is-empty)
@@ -249,6 +225,10 @@ qa-list.md:问题 (http://stackoverflow.com/questions/952914/making-a-flat-list-
249225

250226
qa-list.md:问题 (http://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map)
251227

228+
qa-list.md:问题 (http://stackoverflow.com/questions/626759/whats-the-difference-between-list-and-tuples)
229+
230+
qa-list.md:问题 (http://stackoverflow.com/questions/17160162/what-is-in-python-2-7)
231+
252232
qa-math.md:问题 (http://stackoverflow.com/questions/1476/how-do-you-express-binary-literals-in-python)
253233

254234
qa-math.md:问题 (http://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python)
@@ -257,6 +237,16 @@ qa-math.md:问题 (http://stackoverflow.com/questions/1267869/how-can-i-force-di
257237

258238
qa-math.md:问题 (http://stackoverflow.com/questions/961632/converting-integer-to-string-in-python)
259239

240+
qa-math.md:问题 (http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers)
241+
242+
qa-math.md:问题 (http://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points)
243+
244+
qa-math.md:问题 (http://stackoverflow.com/questions/432842/how-do-you-get-the-logical-xor-of-two-variables-in-python)
245+
246+
qa-math.md:问题 (http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python)
247+
248+
qa-math.md:问题 (http://stackoverflow.com/questions/1485841/behaviour-of-increment-and-decrement-operators-in-python)
249+
260250
qa-modules.md:问题 (http://stackoverflow.com/questions/448271/what-is-init-py-for)
261251

262252
qa-modules.md:问题 (http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path)
@@ -267,6 +257,14 @@ qa-modules.md:问题 (http://stackoverflow.com/questions/44834/can-someone-expla
267257

268258
qa-modules.md:问题 (http://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module)
269259

260+
qa-modules.md:问题 (http://stackoverflow.com/questions/36932/how-can-i-represent-an-enum-in-python)
261+
262+
qa-modules.md:问题 (http://stackoverflow.com/questions/419163/what-does-if-name-main-do)
263+
264+
qa-modules.md:问题 (http://stackoverflow.com/questions/279237/import-a-module-from-a-relative-path)
265+
266+
qa-modules.md:问题 (http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python)
267+
270268
qa-oop.md:问题 (http://stackoverflow.com/questions/2709821/python-self-explained)
271269

272270
qa-oop.md:问题 (http://stackoverflow.com/questions/70528/why-are-pythons-private-methods-not-actually-private)
@@ -297,6 +295,20 @@ qa-oop.md:问题 (http://stackoverflow.com/questions/372042/difference-between-a
297295

298296
qa-oop.md:问题 (http://stackoverflow.com/questions/472000/python-slots)
299297

298+
qa-oop.md:问题 (http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python)
299+
300+
qa-oop.md:问题 (http://stackoverflow.com/questions/6618002/python-property-versus-getters-and-setters)
301+
302+
qa-oop.md:问题 (http://stackoverflow.com/questions/904036/chain-calling-parent-constructors-in-python)
303+
304+
qa-oop.md:问题 (http://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python)
305+
306+
qa-oop.md:问题 (http://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object)
307+
308+
qa-oop.md:问题 (http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python/6581949#6581949)
309+
310+
qa-oop.md:问题 (http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument)
311+
300312
qa-others.md:问题 (http://stackoverflow.com/questions/170921/should-i-learn-python-2-before-3-or-start-directly-from-python-3)
301313

302314
qa-others.md:问题 (http://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv)
@@ -319,6 +331,14 @@ qa-others.md:问题 (http://stackoverflow.com/questions/89228/calling-an-externa
319331

320332
qa-others.md:问题 (http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python)
321333

334+
qa-others.md:问题 (http://stackoverflow.com/questions/261638/how-do-i-protect-python-code)
335+
336+
qa-others.md:问题 (http://stackoverflow.com/questions/191673/preferred-python-unit-testing-framework)
337+
338+
qa-others.md:问题 (http://stackoverflow.com/questions/61151/where-do-the-python-unit-tests-go)
339+
340+
qa-others.md:问题 (http://stackoverflow.com/questions/6344076/differences-between-distribute-distutils-setuptools-and-distutils2)
341+
322342
qa-pip-easy_install.md:问题 (http://stackoverflow.com/questions/2720014/upgrading-all-packages-with-pip)
323343

324344
qa-pip-easy_install.md:问题 (http://stackoverflow.com/questions/1231688/how-do-i-remove-packages-installed-with-pythons-easy-install)
@@ -331,6 +351,8 @@ qa-pip-easy_install.md:问题 (http://stackoverflow.com/questions/739993/get-a-l
331351

332352
qa-pip-easy_install.md:问题 (http://stackoverflow.com/questions/3220404/why-use-pip-over-easy-install)
333353

354+
qa-pip-easy_install.md:问题 (http://stackoverflow.com/questions/4324558/whats-the-proper-way-to-install-pip-virtualenv-and-distribute-for-python)
355+
334356
qa-std-modules.md:问题 (http://stackoverflow.com/questions/712791/json-and-simplejson-module-differences-in-python)
335357

336358
qa-std-modules.md:问题 (http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python)
@@ -347,6 +369,12 @@ qa-std-modules.md:问题 (http://stackoverflow.com/questions/2018026/should-i-us
347369

348370
qa-std-modules.md:问题 (http://stackoverflow.com/questions/192109/is-there-a-function-in-python-to-print-all-the-current-properties-and-values-of)
349371

372+
qa-std-modules.md:问题 (http://stackoverflow.com/questions/132058/showing-the-stack-trace-from-a-running-python-application)
373+
374+
qa-std-modules.md:问题 (http://stackoverflow.com/questions/1549801/differences-between-isinstance-and-type-in-python)
375+
376+
qa-std-modules.md:问题 (http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python)
377+
350378
qa-string.md:问题 (http://stackoverflow.com/questions/493819/python-join-why-is-it-string-joinlist-instead-of-list-joinstring)
351379

352380
qa-string.md:问题 (http://stackoverflow.com/questions/6797984/how-to-convert-string-to-lowercase-in-python)
@@ -390,6 +418,8 @@ more [link](https://github.com/wklken/stackoverflow-py-top-qa/graphs/contributor
390418

391419
You can Buy me a coffee:) [link](http://www.wklken.me/pages/donation.html)
392420

421+
422+
393423
------
394424

395425

contents/qa-built-in.md

+56
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,59 @@ Are you talking about lambda functions? Like
232232

233233
lambda function may be the shortest way to write something out
234234

235+
### Python中__str____repr__的区别
236+
237+
问题[链接](http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python)
238+
239+
Alex总结的很好,但是,有点出乎意料的精简了。
240+
241+
首先,让我重复一下Alex回答的重点:
242+
243+
- 缺省实现是没用的(很难想象,但是的确是这样的)
244+
245+
- `__repr__`的目的是清晰
246+
247+
- `__str__`的目的是可读性
248+
249+
- 容器的`__str__`使用已包含对象`__repr__`
250+
251+
**缺省实现是没用的**
252+
253+
这实在是奇怪,因为Python的缺省实现是为了完全的可用。然而,在这种情况下,使用缺省的`__repr__`会表现的像这样:
254+
255+
return "%s(%r)" % (self.__class__, self.__dict__)
256+
257+
这很危险(举个例子。如果对象被引用,太容易陷入无限循环)。所以,Python选择逃避。注意有一个缺省是正确的:如果`__repr__`已经定义,而`__str__`没有,对象会表现出`__str__=__repr__`
258+
259+
这意味着,简单讲:几乎所有你实现的对象都应该有一个`__repr__`函数用来理解这个对象。实现`__str__`是一个选择:如果你需要一个"良好的打印”函数(举个例子,用来表现一个生成器)
260+
261+
**`__repr__`的目标是清晰的**
262+
263+
让我实话实说-我并不相信调试器。我不知道如何使用任何一种调试器,而且没有认真使用过任何一款。我觉的调试器的最大错误是它们的本质--我调试错误发生在很久很久以前,超级久远。这意味着我有着宗教热情一般的相信日志。日志是一切一劳永逸的服务器系统的生命之血。Python可以轻松的记录日志:可能某些项目有特殊的包装,但是你需要的仅仅是一句:
264+
265+
(INFO, "I am in the weird function and a is", a, "and b is", b, "but I got a null C — using default", default_c)
266+
267+
但是你需要做最后一步,所有你实现的对象有一个有效的repr,所以上面这种代码才会工作。这就是为什么"eval"这种东西出现:如果你又足够的信息可以让`eval(repr(c)) == c`,这意味着你知道所有的东西,像知道`c`一样。如果它足够简单,至少通过模糊的方法,实现它。如果不是这样的,请无论如何了解`c`的足够信息。我通常使用一个类eval的格式:`"My class(this=%r, that=%r)" % (self.this, self.that)"`。这不意味着你可以真正的结构化MyClass或者他们全都是正确的结构器参数。但是他们是很有用的形式代表“这个实例中你需要了解的信息就是这些”。
268+
269+
注意:我用的是`%r`而不是`%s`。你总是想用`repr()`[或者`%r`格式等效的角色]在一个可实现的`__repr__`中,或者你被repr的目的打败的。你需要知道`MyClass(3)``Myclass("3")`的区别。
270+
271+
**`__str__`的目标是可读性**
272+
273+
实际上,它不是为了更清晰--注意`str(3) == str("3")`。同样的,如果你实现了一个IP的抽象,有一个字符串看上去像192.168.1.1是Ok的。当实现一个日期或时间的抽象时,字符串可以是“2014/4/12 15:35:33"等等。所以它的目标是通过一种方式让用户,而不是一个程序员,可以正常的阅读它。去掉那些无用的字码,假装成其他的类--在它支持可读性之后,这是一种进化。
274+
275+
**容器的`__str__`使用已包含对象`__repr__`
276+
277+
这看上去很奇怪,是不是?确实有一点,但是可读性是这样:
278+
279+
[moshe is, 3, hello
280+
world, this is a list, oh I don't know, containing just 4 elements]
281+
282+
吗?不一定。尤其是,容器内部的字符串会找到一个很容易实现的方式去构建它们所代表的东西。面对这种含糊不清的东西,记住,Python禁止猜测。如果你打印一个列表,想要上述的表现形式时,只需要
283+
284+
print "["+", ".join(l)+"]"
285+
286+
(你应该会想出如何处理字典)
287+
288+
**总结**
289+
290+
为你所有实现的类,实现`__repr__`。这应该是第二特性。实现`__str__`,对于那些你认为使用可视化字符串会更好的表现出错误的可读性,并阻止含糊不清的东西。

contents/qa-control-flow.md

+31
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,35 @@ why or why not?
287287
- xrange()并不适用于所有情况, 例如, 不支持slices以及任意的list方法
288288

289289

290+
### 在Python中,“i += x”和“i = i + x”什么时候不等
290291

292+
问题[链接](http://stackoverflow.com/questions/15376509/when-is-i-x-different-from-i-i-x-in-python)
293+
294+
这完全取决于i这个对象。
295+
296+
`+=`调用了[`__iadd__`方法](https://docs.python.org/2/reference/datamodel.html#object.__iadd__)(如果存在—不存在就退一步调用`__add__`),然而`+`调用[`__add__`方法]https://docs.python.org/2/reference/datamodel.html#object.__add__)^1
297+
298+
从一个API的角度,`__iadd__`期望被使用在恰当的位置修改易变的对象(返回的对象也是转变后的),而`__add__`应该返回某些东西的一个新的实例。对于不可变对象,两种方法都返回新的实例,但`__iadd__`会把新的实例放在和旧实例名字相同的命名空间里。这就是为什么
299+
300+
i = 1
301+
i += 1
302+
303+
看上去增量`i`,实际上,你得到了一个新的数值,并且转移到了`i`的最上面—丢掉了旧的数值的引用。在这种情况下,`i += 1``i = i + 1`是完全一样的。但是,对于大多数可变对象,这是完全不同的:
304+
305+
一个具体的例子:
306+
307+
a = [1, 2, 3]
308+
b = a
309+
b += [1, 2, 3]
310+
print a #[1, 2, 3, 1, 2, 3]
311+
print b #[1, 2, 3, 1, 2, 3]
312+
313+
对比一下:
314+
315+
a = [1, 2, 3]
316+
b = a
317+
b = b + [1, 2, 3]
318+
print a #[1, 2, 3]
319+
print b #[1, 2, 3, 1, 2, 3]
320+
321+
注意第一个例子,从`b``a`代表相同的对象开始,当我对`b`使用`+=`,实际上它改变了`b``a`看起来也改变了- -毕竟他们代表同一个列表)。但是在第二个例子里,当我进行`b = b + [1, 2, 3]`操作时,b被引用并且和一个新的列表`[1, 2, 3]`联系了起来。之后在b的命名空间保存了这个关联的列表- -不考虑b之前的序列。

0 commit comments

Comments
 (0)