Python Module Exposure

Jacob Page apoco at cox.net
Sat Jul 9 21:15:55 EDT 2005


George Sakkis wrote:
> "Jacob Page" <apoco at cox.net> wrote:
> 
>>I think I will keep Interval exposed.  It sort of raises a bunch of
>>hard-to-answer design questions having two class interfaces, though.
>>For example, would Interval.between(2, 3) + Interval.between(5, 7) raise
>>an error (as it currently does) because the intervals are disjoint or
>>yield an IntervalSet, or should it not even be implemented?  How about
>>subtraction, xoring, and anding?  An exposed class should have a more
>>complete interface.
>>
>>I think that IntervalSet.between(5, 7) | IntervalSet.between(2, 3) is
>>more intuitive than IntervalSet(Interval.between(5, 7),
>>Interval.between(2, 3)), but I can understand the reverse.  I think I'll
>>just support both.
> 
> As I see it, there are two main options you have:
> 
> 1. Keep Intervals immutable and pass all the responsibility of
> combining them to IntervalSet. In this case Interval.__add__ would have
> to go. This is simple to implement, but it's probably not the most
> convenient to the user.
> 
> 2. Give Interval the same interface with IntervalSet, at least as far
> as interval combinations are concerned, so that Interval.between(2,3) |
> Interval.greaterThan(7) returns an IntervalSet. Apart from being user
> friendlier, an extra benefit is that you don't have to support
> factories for IntervalSets, so I am more in favor of this option.

I selected option one; Intervals are immutable.  However, this doesn't 
mean that __add__ has to go, as that function has no side-effects.  The 
reason I chose option one was because it's uncommon for a mathematical 
operation on two objects to return a different type altogether.

> Another hard design problem is how to combine intervals when
> inheritance comes to play. Say that you have FrozenInterval and
> FrozenIntervalSet subclasses. What should "Interval.between(2,3) |
> FrozenInterval.greaterThan(7)" return ?

For now, operations will return mutable instances.  They can always be 
frozen later if needs be.



More information about the Python-list mailing list