Real-world use cases for map's None fill-in feature?

Raymond Hettinger python at rcn.com
Mon Jan 9 04:16:13 EST 2006


[Anders Hammarquist]:
> I had a quick look through our (Strakt's) codebase and found one example.

Thanks for the research :-)


> The code is used to process user-designed macros, where the user wants
> to append data to strings stored in the system. Note that all data is
> stored as lists of whatever the relevant data type is.
>
> While I didn't write this bit of code (so I can't say what, if any,
> alternatives were considered), it does seem to me the most straight-
> forward way to do it. Being able to say what the fill-in value should
> be would make the code even simpler.
>
> oldAttrVal is the original stored data, and attValue is what the macro
> wants to append.
>
>     newAttrVal = []
>     for x, y in map(None, oldAttrVal, attrValue):
>         newAttrVal.append(u''.join((x or '', y or '')))

I'm finding this case difficult to analyze and generalize without
knowing the significance of position in the list.  It looks like None
fill-in is used because attrValue may be a longer list whenever the
user is specifying new system strings and it may be shorter when some
of there are no new strings and the system strings aren't being updated
at all.  Either way, it looks like the ordinal position has some
meaning that is shared by both oldAttrVal and newAttrVal, perhaps a
message number or somesuch.  If that is the case, is there some other
table the assigns meanings to the resulting strings according to their
index?  What does the code look like that accesses newAttrVal and how
does it know the significance of various positions in the list?  This
is important because it could shed some light on how an app finds
itself looping over two lists which share a common meaning for each
index position, yet they are unequal in length.



Raymond




More information about the Python-list mailing list