31

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
Mark
  • 1,637
  • 4
  • 16
  • 18
  • 10
    It's just a container - anything that's hashable is valid - why would specific values be of any concern to PEP8? – Jon Clements Nov 20 '12 at 14:09

4 Answers4

31

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.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 3
    This answer may give the correct "final" answer (yes, spaces are allowed in dictionary key strings), but the reasoning is incorrect. The reason that spaces are allowed inside dictionary key strings has nothing to do with a style guide. See the comment by Jon Clements above (with the original question): "[Python dictionaries are] just a container - anything that's hashable is valid - why would specific values be of any concern to PEP8?" – David J. Oct 02 '19 at 03:27
  • Well how can i call M_D1." foo bar " ; – Chris P Sep 20 '20 at 18:14
  • @ChrisP Using the `__getitem__` notation: `M_D1[" foo bar "]` – Ashwini Chaudhary Sep 21 '20 at 02:43
  • @AshwiniChaudhary yes that's it. It's little weird because there are two calls: a) M_D1[" foo bar "] b) M_D1.one_key. When we can't use the second one, we can use the first. – Chris P Sep 22 '20 at 12:16
11

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
5

If your keys are strings, they may contain anything.

What should they represent? Give an example.

eumiro
  • 207,213
  • 34
  • 299
  • 261
  • 1
    I added clarification. This isn't a question of functionality, but more of best practice/style. – Mark Nov 20 '12 at 16:49
2

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.

Emil Ivanov
  • 37,300
  • 12
  • 75
  • 90