Array of dict or lists or ....?

Tim Chase python.list at
Tue Oct 7 03:52:29 CEST 2008

>>    __repr__ = __str__
> I don't know if that's a good practice.

I've seen it in a couple places, and it's pretty explicit what 
it's doing.

>>      try:
>>        data.setdefault(
>>          state, {}).setdefault(
>>          county, {}).setdefault(
>>          cls, Attendence(max_students)).accrue(enrolled)
>>      except TooManyAttendants:
> I suggest to decompress that part a little, to make it a little more
> readable.

I played around with the formatting and didn't really like any of 
the formatting I came up with.  My other possible alternatives were:

     data \
       .setdefault(state, {}) \
       .setdefault(county, {}) \
       .setdefault(cls, Attendence(max_students)) \
   except TooManyAttendants:


       .setdefault(state, {})
       .setdefault(county, {})
       .setdefault(cls, Attendence(max, 0))
   except TooManyAttendants:

Both accentuate the setdefault() calls grouped with their 
parameters, which can be helpful.  Which one is "better" is a 
matter of personal preference:

  * no extra characters but hard to read
  * backslashes, or
  * an extra pair of parens


More information about the Python-list mailing list