简体   繁体   English

如何在python中定义自己的前缀和后缀运算符

[英]how do I define my own prefix and postfix operators in python

This question is an extension of a previous question ( Python: defining my own operators? ). 这个问题是上一个问题的扩展( Python:定义我自己的运算符? )。 I really liked the solution provided there for Infix operators, but for my expressions, I need a way to define custom unary operators in a similar fashion. 我真的很喜欢那里为Infix运算符提供解决方案 ,但是对于我的表达式,我需要一种以类似方式定义自定义一元运算符的方法。

Do you have any recommendations? 你有什么建议? Reusing the existing python operators doesn't help, as I've used them all up. 重用现有的python运算符无济于事,因为我已经用光了它们。 Thank you very much for your help! 非常感谢您的帮助!

The main reason for doing this overloading is the absence of the following unary operators in Python: (Is > 0) (Is >= 0) I need operators to distinguish between these two types of operations and my requirement is to match my interface as closely as possible with the interface provided by a pre-defined language which comes with its own set of operators. 进行此重载的主要原因是Python中缺少以下一元运算符:(Is> 0)(Is> = 0)我需要运算符来区分这两种类型的运算,而我的要求是使我的接口尽可能紧密地匹配尽可能使用预定义语言提供的界面,该语言带有自己的一组运算符。 I could have chosen to replace the operators with > 0 and >= 0 but this did not go down very well with the user community. 我本来可以选择用> 0和> = 0代替运算符,但这在用户社区中并不是很好。 Is there a better way to do this? 有一个更好的方法吗?

Well you can use the same hack: 好吧,您可以使用相同的技巧:

#! /usr/bin/python3.2

class Postfix:
    def __init__(self, f):
        self.f = f

    def __ror__(self, other):
        return self.f(other)

x = Postfix(lambda x: x * 2)

a = 'Hello'
print(a |x)
a = 23
print(a |x |x)

Nevertheless, I wouldn't advocate its use, as it is only confusing. 但是,我不会主张使用它,因为这只会造成混淆。

EDIT: Especially as your operators are unary, you can simply call a function, and anyone reading your code would understand immediately what it does. 编辑:尤其是当您的运算符是一元运算符时,您可以简单地调用一个函数,并且任何阅读您的代码的人都会立即理解它的作用。

def choose(t): pass
    #magic happens here and returns nCr(t[0], t[1])

nCr = Postfix(choose)

#This is unintuitive:
print((3, 4) |nCr)

nCr = choose

#But this is obvious:
print(nCr((3, 4)))

Edit2: Dear people who are religious about PEP-8: This "operator"-hack is all about not complying with PEP-8, so please stop editing the answer. Edit2:亲爱的对PEP-8持虔诚态度的人:此“操作员”黑客是关于不遵守PEP-8的全部内容,因此请停止编辑答案。 The idea is that |op is read like one entity, basically a postfix operator. 想法是|op像一个实体一样被读取,基本上是一个后缀运算符。


Edit 3: Thinking hard about a case where this hack could come in handy, maybe the following could be a halfway sensible use. 编辑3:认真考虑这种黑客可能派上用场的情况,也许以下做法可能是明智之举。 (If and only if this feature is well documented in the API): (且仅当此功能在API中有详细说明时):

#! /usr/bin/python3.2

class Language:
    def __init__(self, d):
        self.d = d

    def __ror__(self, string):
        try: return self.d[string]
        except: return string

enUS = Language({})
esMX = Language({'yes': 'sí', 'cancel': 'cancelar'})
deDE = Language({'yes': 'ja', 'no': 'nein', 'cancel': 'abbrechen'})

print('yes' |enUS)
print('no' |deDE)
print('cancel' |esMX)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM