1

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.

Community
  • 1
  • 1
bjd2385
  • 2,013
  • 4
  • 26
  • 47
  • The exception you're getting suggests that `__float__` is expected to return a single float value and that doing something else is not acceptable. See [the docs](https://docs.python.org/3/reference/datamodel.html?highlight=__float__#object.__complex__). – Blckknght Jul 23 '16 at 03:12
  • Looking at your gist, and the (as yet empty) `multioperalist` class: I hope you're not trying to re-invent numpy, but are doing this as an exercise. –  Jul 23 '16 at 03:15
  • @Evert I'm not, don't worry. It's just to understand how all of this stuff works – bjd2385 Jul 23 '16 at 03:18
  • @Evert Even if much of the same functionality were the same (again that's not my goal, it's just to understand how magic methods work and may be overridden, etc.), why should everything be a NumPy array? Or is it just for the readibility of others, since it's the standard for array manipulation – bjd2385 Jul 23 '16 at 04:03
  • What do you mean with "why should everything be a NumPy array?". Do you mean: why should everyone use NumPy in their code, and not another solution? Or do you mean: why can't some things be lists instead? –  Jul 23 '16 at 19:21
  • @Evert perhaps both. But I understand NumPy is heavily optimized, so I guess either way it just makes sense as one would be cluttering their code with nonsensical additions to lists or have to import an external file anyway – bjd2385 Jul 23 '16 at 19:25
  • 1
    Correct: NumPy's optimized and very well tested. That's why I asked. Of course, there'll always be use for lists. And it's definitely useful as practice to mimic something like a numpy array. In the end though, there are often better (tested) solutions out there than one's own crafted solutions (though it sometimes requires rethinking and recoding your code. E.g., numpy arrays don't have a simple `append()` method). Enjoy. –  Jul 24 '16 at 00:42

1 Answers1

3

Unfortunately, you can't. I can't find any precise wording on this, but the documentation states:

Called to implement the built-in functions complex(), int(), float() and round(). Should return a value of the appropriate type.

I read that as that __float__() should return a float, since float():

[r]eturn[s] a floating point number constructed from a number or string x.


For comparison, numpy also doesn't do this. Instead, it has a method astype(<type>) to its ndarray that converts to the specific <type>.
That confirms to me that this indeed cannot be done.

  • Okay, this makes sense. And further, for the record, I'm definitely *not* trying to re-create NumPy lol. It's just cool, nothing more – bjd2385 Jul 23 '16 at 03:20