Graph library for Python (was: Re: python bijection)

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Dec 7 17:48:22 EST 2009


On Mon, 07 Dec 2009 17:23:24 -0500, geremy condra wrote:


>>  * Graph.__iter__ could be mapped to an iterator using
>>   the fastest traversal method for the graph nodes (ie. order does not
>>   matter, it's only important that all nodes are found as fast as
>>   possible)
> 
> Again, it seems ambiguous as to whether nodes or edges are the intended
> target here, and while the API can obviously dictate that, it seems a
> bit like a case of "in the face of ambiguity, refuse the temptation to
> guess" to me.

Consider dicts. They have methods that iterates over keys, values, and 
(key, value) pairs, but if you just iterate over the dict itself, you get 
the keys. This was a deliberate design decision.

It's not a matter of guessing in the face of ambiguity, but giving an 
easy way to get the most common case. Assuming that iteration over nodes 
is more common than iteration over edges, then iter(graph) should yield 
nodes in some unspecified order. If you need the nodes in a particular 
order, then you will call some method that guarantees the order, and if 
you want the edges, you call a method that gives edges.

If you're not sure whether wanting nodes or edges will be more common, 
then you should wait until you, or the community, has more real-world 
experience with the module. Python had dicts for something approaching a 
decade before they became directly iterable.



-- 
Steven



More information about the Python-list mailing list