x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up

Chris Angelico rosuav at gmail.com
Sun Jan 31 08:27:48 EST 2016


On Sun, Jan 31, 2016 at 9:14 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> Imagine taking a large and complex JSON blob
>> and loading it into a Python structure with nested lists and dicts -
>> it wouldn't violate software design principles to call up
>> info["records"][3]["name"], even though that's three indirections in a
>> row. Parsing HTML is even worse, as there's generally going to be
>> numerous levels of structure that have no semantic meaning (they're
>> there for layout) - so instead of three levels, you might easily have
>> a dozen.
>
> This might not be a Law of Demeter violation, but it's certain a violation
> of "Flat Is Better Than Nested".

Oh, absolutely! But "Flat is better than nested" is a principle of
design, and when you're parsing someone else's data structure, you
follow their design, not yours. This isn't the best of designs, but
it's what works with the file he has.

ChrisA



More information about the Python-list mailing list