New submission from Martin Häcker <spamfaen...@gmx.de>:

Code that uses higher order methods is often the clearest description of what 
you want to do. However since the higher order methods in python (filter, map, 
reduce) are free functions and aren't available on collection classes as 
methods, using them creates really hard to read code.

For example, if I want to get an attribute out of an array of objects and 
concatenate the results it's two steps, get the attributes out of the object, 
then concatenate the results.

Without higher order methods its like this. Uses three lines, intermediate 
variable is quite clear, but hard to compose and hard to abstract out parts of 
it.

 self.questions = []
 for topic in self.topics:
   self.questions.append(topic.questions)

if I want to do it with higher order functions there's really two ways, do it 
in one step with reduce:
 self.questions = reduce(lambda memo, topic: memo + topic.questions, 
self.topics, [])

Or use two steps with the operator module
 self.questions = reduce(operator.add, map(operator.attrgetter('questions'), 
self.topics), [])

Of these thee first still couples two steps into one lambda, while the second 
one decoples everything nicely but is a total train wreck to read, as you need 
to constantly jump back and forth in the line to understand what it does.

Having map and reduce defined on collections would not only make it drastically 
shorter but also allows you to read it from front to back in one go. (Ok, there 
is still the caveat that the assignment is in the front instead of at the end, 
but hey)

 self.questions = self.topics.map(attrgetter('questions')).reduce(add, [])

That would be nicely separated into individual steps that exactly describe what 
each step is actually doing, is easy to abstract over and actually very 
succinct.

----------
messages: 151435
nosy: dwt
priority: normal
severity: normal
status: open
title: Python library structure creates hard to read code when using higher 
order functions
type: enhancement

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue13804>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to