I'm trying to figure out how to give an object the ability to unpack values.
The use case I came up with is the following:
- Let's have an Interval class, which we'll use to evaluate real valued functions.
- We would like to ask for
- Membership, hence
__contains__. - Iterate over it, by calling with an specific step, or just using a default step, hence the
__call__, and__iter__.
- Membership, hence
- In the future I'll support union, and intersection.
One of the features I'm looking for, is to be able to call an Interval object and unpack it, such that if I = Interval(1, 2) and a, b = *I then a == 1 and b == 2.
This is mainly to work in functions that will have interval limits in their arguments.
For example a function def Integrate(f, a, b): ... and then we evaluate, integral = Integrate(f, *I) where $f$ real valued function and I is the interval we were discussing.
The problem is that I'm not entirely sure which data model method should I use.
So far the example class I came up with is the following.
class Interval:
def __init__(self, a, b):
if a >= b:
raise Exception("Invalid values for an interval")
self.upper = b
self.lower = a
def __contains__(self, value):
return self.lower <= value <= self.upper
def __call__(self, h=_h):
yield from dense_range(self.lower, self.upper, h)
def __iter__(self):
return self()
def __repr__(self):
return f"<Interval [{self.lower}, {self.upper}]>"
Any ideas?
I don't have a lot of experience implementing iterators, but I believe all you need is to yield the relevant items from
__iter__:Some uses: