Spaces in Python Dictionary Keys
Question:
I know you can have spaces in Python dictionary keys, but is that considered bad programming? I couldn’t find anything in the PEPs about this.
Edit for clarification:
On a project I’m doing, I’m working on something that parses the scoreboard output from Apache’s mod_status
(see example output below.) I’m just trying to figure out the best practice. Should I end up with this:
workers = {'W': 1,
'_': 9,
...}
or this:
workers = {'Sending Reply': 1,
'Waiting for Connection': 9,
...}
Example mod_status
output:
_....___W____._.................................................
................................................................
................................................................
................................................................
Scoreboard Key:
"_" Waiting for Connection, "S" Starting up, "R" Reading Request,
"W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
"C" Closing connection, "L" Logging, "G" Gracefully finishing,
"I" Idle cleanup of worker, "." Open slot with no current process
Answers:
If your keys are strings, they may contain anything.
What should they represent? Give an example.
Your keys can be any (string) you want. If you are using them for something internal – a space might not be the best idea. Otherwise – ok.
You may be thinking of Javascript, where you can refer to an object via its keys with both obj.foo
and obj['foo']
. In this case, spaces could be considered bad form because the first version will not work: Python doesn’t have that option in the first place, so the question doesn’t arise.
You can use tools like pylint to check whether your code is PEP8 compatible or not :
so I tried something like :
M_D1 = {" foo bar ":1, " a bc":2, " ":3}
and pylint gave me 10/10 rating, so I guess it has no issues with the spaces used in keys.
I know you can have spaces in Python dictionary keys, but is that considered bad programming? I couldn’t find anything in the PEPs about this.
Edit for clarification:
On a project I’m doing, I’m working on something that parses the scoreboard output from Apache’s mod_status
(see example output below.) I’m just trying to figure out the best practice. Should I end up with this:
workers = {'W': 1,
'_': 9,
...}
or this:
workers = {'Sending Reply': 1,
'Waiting for Connection': 9,
...}
Example mod_status
output:
_....___W____._.................................................
................................................................
................................................................
................................................................
Scoreboard Key:
"_" Waiting for Connection, "S" Starting up, "R" Reading Request,
"W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
"C" Closing connection, "L" Logging, "G" Gracefully finishing,
"I" Idle cleanup of worker, "." Open slot with no current process
If your keys are strings, they may contain anything.
What should they represent? Give an example.
Your keys can be any (string) you want. If you are using them for something internal – a space might not be the best idea. Otherwise – ok.
You may be thinking of Javascript, where you can refer to an object via its keys with both obj.foo
and obj['foo']
. In this case, spaces could be considered bad form because the first version will not work: Python doesn’t have that option in the first place, so the question doesn’t arise.
You can use tools like pylint to check whether your code is PEP8 compatible or not :
so I tried something like :
M_D1 = {" foo bar ":1, " a bc":2, " ":3}
and pylint gave me 10/10 rating, so I guess it has no issues with the spaces used in keys.