This post originated from an RSS feed registered with Agile Buzz
by Keith Ray.
Original Post: Returning Multiple Values
Feed Title: MemoRanda
Feed URL: http://homepage.mac.com/1/homepage404ErrorPage.html
Feed Description: Keith Ray's notes to be remembered on agile software development, project management, oo programming, and other topics.
One of the neato things about Python is returning a small number of multiple values from a method is easy. For example, finding the elements that are shared by two collections/lists [hope I got the syntax right]:
beforeNotAfter, afterNotBefore = foo.findNonOverlappedElements( before, after )
# do stuff using beforeNotAfter
# do stuff using afterNotBefore
Alan Knight says the Smalltalk "moral equivalent" to this uses a block (my paraphrasing here):
foo findNonOverlappedElementsOf: before and: after
doing: [ : beforeNotAfter : afterNotBefore
"do stuff to beforeNotAfter".
"do stuff to afterNotBefore" ].
Coming from a background of C/Object-Pascal/Objective-C/Java/C++, it's hard for me to think of reasons to do a Python-style multiple-return; instead of one method that returned two values, I'd write two methods, or single method that returns a data-structure or has output-parameters. And, unfortunately, Objective C, Java, etc., don't make "blocks" easy. Though there is a dialect of Objective-C that has blocks, it's not the dialect supported by gcc and Apple.