0

Grouping dictionary keys by a prefix is useful when exploring its structure. However, what to do with keys that cannot be easily grouped / are miscellaneous?

mydict = {'_id': '',
          '_nick': '',
          '_address': '',

          'status_destination': '',
          'status_file': '',
          'status_object': '',

          'objective_value': '',
          'objective_result': '',}

Prefix underscore is shorter than something arbitrary like misc. But, underscore has a certain meaning in python. Dictionary keys are object names and as such, they shall not be prefixed by underscore according to PEP-8.

What could be used instead that is not distractive? Ordered dictionary does not always work since misc keys can be entered at various times.

https://peps.python.org/pep-0008/

What is the meaning of single and double underscore before an object name?

aeiou
  • 337
  • 1
  • 7

1 Answers1

1

This is probably a matter of preference, but my suggestions are:

  1. use CamelCase for key name and single_underscore as delimiter to group prefix. No underscore means miscellaneous.
  2. use single_underscores for key name and double__underscore as delimiter to group prefix. Note that PEP-8 does only mention double leading or trailing underscores, so a key like status__destination is according to PEP-8.
Rich
  • 46
  • 2
  • If there is no underscore, the miscellaneous keys will be all over the place... The second suggestion implies using smth like misc_key which is quite heavy, more so if dictionary itself is called misc :-). – aeiou Jul 03 '22 at 11:47