Per my question from earlier today (which was wonderfully answered, and I appreciate everyone's insight), I've extended that small class for the heck of it to almost all the operations we'd normally perform upon integers and floats.
Now I'm not certain how to convert all the entries to floats without list comprehensions.
For instance, right now I have the following:
def __float__(self):
return operalist(float(i) for i in self)
But when I call the following sequence of commands:
>>> from operalist import operalist
>>> X = operalist(range(1, 11))
>>> X
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> float(X)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __float__ returned non-float (type operalist)
What I would rather see is what we'd get by using a list comprehension:
>>> [float(i) for i in X]
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
How do I add a __float__
method, since it doesn't appear to be native to lists? Unless it's not a good idea.