Difference between operators and methods

后端 未结 3 415
我寻月下人不归
我寻月下人不归 2021-01-06 07:59

Is there any substantial difference between operators and methods?

The only difference I see is the way the are called, do they have other differences?

For

相关标签:
3条回答
  • 2021-01-06 08:38

    If I understand question currectly...

    In nutshell, everything is a method of object. You can find "expression operators" methods in python magic class methods, in the operators.

    So, why python has "sexy" things like [x:y], [x], +, -? Because it is common things to most developers, even to unfamiliar with development people, so math functions like +, - will catch human eye and he will know what happens. Similar with indexing - it is common syntax in many languages.

    But there is no special ways to express upper, replace, strip methods, so there is no "expression operators" for it.

    So, what is different between "expression operators" and methods, I'd say just the way it looks.

    0 讨论(0)
  • 2021-01-06 08:40

    Your question is rather broad. For your examples, concatenation, slicing, and indexing are defined on strings and lists using special syntax (e.g., []). But other types may do things differently.

    In fact, the behavior of most (I think all) of the operators is constrolled by magic methods, so really when you write something like x + y a method is called under the hood.

    From a practical perspective, one of the main differences is that the set of available syntactic operators is fixed and new ones cannot be added by your Python code. You can't write your own code to define a new operator called $ and then have x $ y work. On the other hand, you can define as many methods as you want. This means that you should choose carefully what behavior (if any) you assign to operators; since there are only a limited number of operators, you want to be sure that you don't "waste" them on uncommon operations.

    0 讨论(0)
  • 2021-01-06 08:41

    Is there any substantial difference between operators and methods?

    Practically speaking, there is no difference because each operator is mapped to a specific Python special method. Moreover, whenever Python encounters the use of an operator, it calls its associated special method implicitly. For example:

    1 + 2
    

    implicitly calls int.__add__, which makes the above expression equivalent1 to:

    (1).__add__(2)
    

    Below is a demonstration:

    >>> class Foo:
    ...     def __add__(self, other):
    ...         print("Foo.__add__ was called")
    ...         return other + 10
    ...
    >>> f = Foo()
    >>> f + 1
    Foo.__add__ was called
    11
    >>> f.__add__(1)
    Foo.__add__ was called
    11
    >>>
    

    Of course, actually using (1).__add__(2) in place of 1 + 2 would be inefficient (and ugly!) because it involves an unnecessary name lookup with the . operator.

    That said, I do not see a problem with generally regarding the operator symbols (+, -, *, etc.) as simply shorthands for their associated method names (__add__, __sub__, __mul__, etc.). After all, they each end up doing the same thing by calling the same method.


    1Well, roughly equivalent. As documented here, there is a set of special methods prefixed with the letter r that handle reflected operands. For example, the following expression:

    A + B
    

    may actually be equivalent to:

    B.__radd__(A)
    

    if A does not implement __add__ but B implements __radd__.

    0 讨论(0)
提交回复
热议问题