[Python-ideas] π = math.pi

Stephan Houben stephanh42 at gmail.com
Sat Jun 3 02:32:27 EDT 2017


Hi Joshua,

>     A `op` B
>
> is equivalent to
>
>     op(A, B

This can of course be faked in Python.

https://gist.github.com/stephanh42/a4d6d66b10cfecf935c9531150afb247

Now you can do:

========
@BinopCallable
def add(x, y):
      return x + y

print(3 @add@ 5)
===========

Stephan

2017-06-03 7:59 GMT+02:00 Joshua Morton <joshua.morton13 at gmail.com>:
> For reference, haskell is perhaps the closest language to providing
> arbitrary infix operators, and it requires that they be surrounded by
> backticks. That is
>
>     A `op` B
>
> is equivalent to
>
>     op(A, B)
>
> That doesn't work for python (backtick is taken) and I don't think anything
> similar is a good idea.
>
> On Sat, Jun 3, 2017 at 1:56 AM Chris Angelico <rosuav at gmail.com> wrote:
>>
>> On Sat, Jun 3, 2017 at 3:42 PM, Pavol Lisy <pavol.lisy at gmail.com> wrote:
>> > Sorry for probably stupid question! Is something like ->
>> >
>> >     class A:
>> >         def __oper__(self, '⊞', other):
>> >             return something(self.value, other)
>> >
>> >     a = A()
>> >     a ⊞ 3
>> >
>> > thinkable?
>>
>> No, because operators need to be defined before you get to individual
>> objects, and they need precedence and associativity. So it'd have to
>> be defined at the compiler level.
>>
>> Also, having arbitrary operators gets extremely confusing. It's not
>> easy to reason about code when you don't know what's even an operator.
>>
>> Not a stupid question, but one for which the answer is "definitely not
>> like that".
>>
>> ChrisA
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


More information about the Python-ideas mailing list