Hacker News new | past | comments | ask | show | jobs | submit login

In Python, "==" calls "__eq__", a class function. The left argument to "==" determines which class is involved. The right argument is just passed into the selected class member function. There's an asymmetry there. Operators are not necessarily commutative.

This can lead to some strange operator semantics in mixed-mode expressions. Whether chained expressions left-associate or right-associate matters.

numpy.array operations do a lot of implicit conversions. You can use Python arrays on many operations that expect a numpy.array. Mixing them works. Mostly.

    python3
    Python 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import numpy
    >>> a1 = numpy.array([1,2,3])

    >>> a2 = [1,2,3]
    >>> a1 == a2
    array([ True,  True,  True])
    >>> a2 == a1
    array([ True,  True,  True])
    >>> a1 == a2 == a1
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    >>> a2 == a2 == a2
    True
    >>> a1 == a1 == a1
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()



Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: