Python 中的一切都是对象,为什么运算符不是? [英] Everything in Python is an object, why operators are not?

查看:53
本文介绍了Python 中的一切都是对象,为什么运算符不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

Python 中的一切都是对象

我们都知道这句话,所有 Pythonistas(包括我)都喜欢它.在这方面,研究运营商很有趣.它们似乎没有对象,例如

<预><代码>>>>type(*) # 或/, +, -, <...

返回SyntaxError:无效语法.

但是,在某些情况下,将它们视为对象会很有用.例如考虑一个像

这样的函数

def 操作(操作数 1、操作数 2、操作符):"""此函数返回运算符定义的两个操作数的运算作为参数"""# 下面这行是无效的python代码,应该只描述函数返回操作数 1 <运算符>操作数2

所以 operation(1, 2, +) 会返回 3operation(1, 2, *) 会返回 2, operation(1, 2, <) 将返回 True 等...

为什么这在python中没有实现?或者是,如果,如何?

<小时>

备注:我知道 operator 模块,这也不适用于上面的示例函数.我也知道人们可以通过一种方式来解决它,例如operations(operand1, operand2, '>') 并通过相应运算符的字符串表示找到所需的操作.但是,我问的是操作员对象不存在的原因,可以作为函数中的参数传递,例如就像所有其他 python 对象一样.

解决方案

Operators 告诉解释器底层方法对提供的对象进行操作,所以它们更像是函数,它们仍然是对象从某种意义上说,您只需要适当的引用即可调用 type.例如,假设您有 Foo.some_method 并且想要查找其类型.您需要正确的引用:type(Foo.some_method) 而不仅仅是 type(some_method),第一个返回 ,后者是一个NameError.

也就是说,你当然可以在没有 operator 模块的情况下实现这样的东西:

def 操作(操作数 1、操作数 2、操作符):返回 getattr(operand1, operator)(operand2)操作(1, 2, '__add__')# 3

也就是说,理解您的问题的最简单方法是,运算符是 Python 解释代码的语法的一部分,而不是实际对象.因此,当解释器看到 *+~ 等时,它需要两个操作数来获取别名方法并执行.方法本身是一个对象.语法,没那么多.

Everything in Python is an object

We all know this sentence and all Pythonistas (including me) loving it. In that regard, it is interesting to look at operators. They seem to be no objects, e.g.

>>> type(*)     # or /, +, -, < ...

returns SyntaxError: invalid syntax.

However, there might be situations where it would be useful to have them treated as objects. Consider for example a function like

def operation(operand1, operand2, operator):
    """
    This function returns the operation of two operands defined by the operator as parameter
    """

    # The following line is invalid python code and should only describe the function
    return operand1 <operator> operand2

So operation(1, 2, +) would return 3, operation(1, 2, *) would return 2, operation(1, 2, <) would return True, etc...

Why is this not implemented in python? Or is it and if, how?


Remark: I do know the operator module, which also wouldn't be applicable in the example function above. Also I am aware that one could workaround it in a way like e.g. operations(operand1, operand2, '>') and find the desired operation via the string representation of the corresponding operator. However I am asking for the reason of the non-existance of operator-objects being able to be passed as parameters in functions e.g. like every other python object.

解决方案

Operators tell the interpreter what underlying method to operate on the objects provided, so they are more like functions, which are still object in a sense, you just need the appropriate reference to call the type on. For instance, say you have Foo.some_method and you want to look up its type. You need the proper reference: type(Foo.some_method) instead of just type(some_method), the first of which returns <class 'function'>, the latter a NameError.

That said, you can certainly implement something like this without the operator module:

def operation(operand1, operand2, operator):
    return getattr(operand1, operator)(operand2)

operation(1, 2, '__add__')
# 3

That said, the easiest way to understand your issue is that operators are part of the syntax for python to interpret your code, not an actual object. So when the interpreter sees *, +, ~ etc... it expects two operands to fetch the aliased method and execute. The method itself is an object. The syntax, not so much.

这篇关于Python 中的一切都是对象,为什么运算符不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆