According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I use double quotes in general, but not for any specific reason - Probably just out of habit from Java.
I guess you're also more likely to want apostrophes in an inline literal string than you are to want double quotes.
I chose to use double quotes because they are easier to see.
Triple quoted comments are an interesting subtopic of this question. PEP 257 specifies triple quotes for doc strings. I did a quick check using Google Code Search and found that triple double quotes in Python are about 10x as popular as triple single quotes -- 1.3M vs 131K occurrences in the code Google indexes. So in the multi line case your code is probably going to be more familiar to people if it uses triple double quotes.
I just use whatever strikes my fancy at the time; it's convenient to be able to switch between the two at a whim!
Of course, when quoting quote characetrs, switching between the two might not be so whimsical after all...
I aim to minimize both pixels and surprise. I typically prefer
'
in order to minimize pixels, but"
instead if the string has an apostrophe, again to minimize pixels. For a docstring, however, I prefer"""
over'''
because the latter is non-standard, uncommon, and therefore surprising. If now I have a bunch of strings where I used"
per the above logic, but also one that can get away with a'
, I may still use"
in it to preserve consistency, only to minimize surprise.Perhaps it helps to think of the pixel minimization philosophy in the following way. Would you rather that English characters looked like
A B C
orAA BB CC
? The latter choice wastes 50% of the non-empty pixels.Python uses quotes something like this:
Which gives the following output: