Asked  7 Months ago    Answers:  5   Viewed   34 times

According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?

 Answers

13

I like to use double quotes around strings that are used for interpolation or that are natural language messages, and single quotes for small symbol-like strings, but will break the rules if the strings contain quotes, or if I forget. I use triple double quotes for docstrings and raw string literals for regular expressions even if they aren't needed.

For example:

LIGHT_MESSAGES = {
    'English': "There are %(number_of_lights)s lights.",
    'Pirate':  "Arr! Thar be %(number_of_lights)s lights."
}

def lights_message(language, number_of_lights):
    """Return a language-appropriate string reporting the light count."""
    return LIGHT_MESSAGES[language] % locals()

def is_pirate(message):
    """Return True if the given message sounds piratical."""
    return re.search(r"(?i)(arr|avast|yohoho)!", message) is not None
Tuesday, June 1, 2021
 
tiny
answered 7 Months ago
94

In C and in C++ single quotes identify a single character, while double quotes create a string literal. 'a' is a single a character literal, while "a" is a string literal containing an 'a' and a null terminator (that is a 2 char array).

In C++ the type of a character literal is char, but note that in C, the type of a character literal is int, that is sizeof 'a' is 4 in an architecture where ints are 32bit (and CHAR_BIT is 8), while sizeof(char) is 1 everywhere.

Tuesday, June 1, 2021
 
codingb
answered 7 Months ago
54

JSON syntax is not Python syntax. JSON requires double quotes for its strings.

Tuesday, June 1, 2021
 
RahulG
answered 7 Months ago
65

PEP 8 kinda predates list comprehensions. I usually break these up over multiple lines at logical locations:

memberdef_list = [elem for elem in from_cache(classname, 'memberdefs')
                  if elem.argsstring != '[]' and 
                     'std::string' in null2string(elem.vartype)]

Mostly though, I'd forgo the involved test there in the first place:

def stdstring_args(elem):
    if elem.argstring == '[]':
        return False
    return 'std::string' in null2string(elem.vartype)

memberdef_list = [elem for elem in from_cache(classname, 'memberdefs')
                  if stdstring_args(elem)]
Wednesday, August 11, 2021
 
Sanguine
answered 4 Months ago
13

As you already mentioned, PEP8 doesn't explicitly mention the slice operator in that format, but spam[3:5] is definitely more common and IMHO more readable.

If pep8 checker is anything to go by, the space before : will be flagged up

[me@home]$ pep8  <(echo "spam[3:44]")   # no warnings
[me@home]$ pep8  <(echo "spam[3 : 44]")  
/dev/fd/63:1:7: E203 whitespace before ':'

... but that's only because of it assumes : to be the operator for defining a literal dict and no space is expected before the operator. spam[3: 44] passes for that reason, but that just doesn't seem right.

On that count, I'd stick to spam[3:44].


Nested arithmetic operations are a little trickier. Of your 3 examples, only the 2nd one passes PEP8 validation:

[me@home]$ pep8 <(echo "spam[ham(66)//3:44+eggs()]")
/dev/fd/63:1:13: E225 missing whitespace around operator

[me@home]$ pep8 <(echo "spam[ham(66) // 3:44 + eggs()]")  # OK

[me@home]$ pep8 <(echo "spam[ham(66) // 3 : 44 + eggs()]")
/dev/fd/63:1:18: E203 whitespace before ':'

However, I find all of the above difficult to parse by eye at first glance.

For readability and compliance with PEP8, I'd personally go for:

 spam[(ham(66) // 3):(44 + eggs())]

Or for more complication operations:

 s_from = ham(66) // 3 
 s_to = 44 + eggs()
 spam[s_from:s_to]
Sunday, August 15, 2021
 
turson
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share