I don't like Python functions that take two or more iterables. Is it a good idea?

Posted by Xavier Ho on Stack Overflow See other posts from Stack Overflow or by Xavier Ho
Published on 2010-05-15T04:55:55Z Indexed on 2010/05/15 5:04 UTC
Read the original article Hit count: 233

This question came from looking at this question on Stackoverflow.

def fringe8((px, py), (x1, y1, x2, y2)):

Personally, it's been one of my pet peeves to see a function that takes two arguments with fixed-number iterables (like a tuple) or two or more dictionaries (Like in the Shotgun API). It's just hard to use, because of all the verbosity and double-bracketed enclosures.

Wouldn't this be better:

>>> class Point(object):
...     def __init__(self, x, y):
...         self.x = x
...         self.y = y
...     
>>> class Rect(object):
...     def __init__(self, x1, y1, x2, y2):
...         self.x1 = x1
...         self.y1 = y1
...         self.x2 = x2
...         self.y2 = y2
...     
>>> def fringe8(point, rect):
...     # ...
...
>>>
>>> point = Point(2, 2)
>>> rect = Rect(1, 1, 3, 3)
>>>
>>> fringe8(point, rect)

Is there a situation where taking two or more iterable arguments is justified? Obviously the standard itertools Python library needs that, but I can't see it being pretty in maintainable, flexible code design.

© Stack Overflow or respective owner

Related posts about python

Related posts about function