Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

can I overload operators like "=>", "->" or something like that?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


dmitrey15 at gmail

Apr 19, 2012, 12:28 PM

Post #1 of 18 (1004 views)
Permalink
can I overload operators like "=>", "->" or something like that?

hi all,
can I somehow overload operators like "=>", "->" or something like
that? (I'm searching for appropriate overload for logical implication
"if a then b")
Thank you in advance, D.
--
http://mail.python.org/mailman/listinfo/python-list


ian.g.kelly at gmail

Apr 19, 2012, 12:51 PM

Post #2 of 18 (998 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On Thu, Apr 19, 2012 at 1:28 PM, dmitrey <dmitrey15 [at] gmail> wrote:
> hi all,
> can I somehow overload operators like "=>", "->" or something like
> that? (I'm searching for appropriate overload for logical implication
> "if a then b")

No, because those aren't operators in Python. You could overload ">="
(__ge__) or ">>" (__rshift__) or "|=" (__ior__). Note that the last one
does an assignment even if you overload it.
--
http://mail.python.org/mailman/listinfo/python-list


jaccarmac at gmail

Apr 19, 2012, 12:54 PM

Post #3 of 18 (996 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On Thursday, April 19, 2012 12:28:50 PM UTC-7, dmitrey wrote:
> hi all,
> can I somehow overload operators like "=>", "->" or something like
> that? (I'm searching for appropriate overload for logical implication
> "if a then b")
> Thank you in advance, D.

I don't believe that you could overload those particular operators, since to my knowledge they do not exist in Python to begin with.
--
http://mail.python.org/mailman/listinfo/python-list


wuwei23 at gmail

Apr 19, 2012, 9:38 PM

Post #4 of 18 (991 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On Apr 20, 5:54am, Jacob MacDonald <jaccar...@gmail.com> wrote:

> On Thursday, April 19, 2012 12:28:50 PM UTC-7, dmitrey wrote:
> > can I somehow overload operators like "=>", "->" or something like
> > that?

> I don't believe that you could overload those particular operators,
> since to my knowledge they do not exist in Python to begin with.

It all depends on if the operators use special methods on objects:
http://docs.python.org/reference/datamodel.html#special-method-names

You can overload => via object.__le__, for example.
--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Apr 19, 2012, 9:43 PM

Post #5 of 18 (991 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On Fri, Apr 20, 2012 at 2:38 PM, alex23 <wuwei23 [at] gmail> wrote:
> On Apr 20, 5:54am, Jacob MacDonald <jaccar...@gmail.com> wrote:
>
>> On Thursday, April 19, 2012 12:28:50 PM UTC-7, dmitrey wrote:
>> > can I somehow overload operators like "=>", "->" or something like
>> > that?
>
>> I don't believe that you could overload those particular operators,
>> since to my knowledge they do not exist in Python to begin with.
>
> It all depends on if the operators use special methods on objects:
> http://docs.python.org/reference/datamodel.html#special-method-names
>
> You can overload => via object.__le__, for example.

Yes, but it will be a comparison operator, and to an extent, will be
assumed to function as one. I don't recommend abusing comparisons for
other operations - you'll confuse people no end.

ChrisA
--
http://mail.python.org/mailman/listinfo/python-list


zahlman at gmail

Apr 19, 2012, 9:43 PM

Post #6 of 18 (992 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On Thu, Apr 19, 2012 at 3:28 PM, dmitrey <dmitrey15 [at] gmail> wrote:
> hi all,
> can I somehow overload operators like "=>", "->" or something like
> that? (I'm searching for appropriate overload for logical implication
> "if a then b")
> Thank you in advance, D.
> --
> http://mail.python.org/mailman/listinfo/python-list


There is no "logical implication" operator in Python, and indeed
Python doesn't really work that way; you can use a condition to
trigger an action, and you can have a conditionally-valued expression,
but you don't tell Python a bunch of facts and then expect it to
reason from those - that's Prolog.

You cannot invent your own operators for Python, either. Again, this
isn't something that programming languages commonly support.

Could you please tell us more about what you're **really** trying to do?

--
~Zahlman {:>
--
http://mail.python.org/mailman/listinfo/python-list


zahlman at gmail

Apr 19, 2012, 9:59 PM

Post #7 of 18 (991 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On Fri, Apr 20, 2012 at 12:43 AM, Chris Angelico <rosuav [at] gmail> wrote:
> On Fri, Apr 20, 2012 at 2:38 PM, alex23 <wuwei23 [at] gmail> wrote:
>> On Apr 20, 5:54am, Jacob MacDonald <jaccar...@gmail.com> wrote:
>>
>>> On Thursday, April 19, 2012 12:28:50 PM UTC-7, dmitrey wrote:
>>> > can I somehow overload operators like "=>", "->" or something like
>>> > that?
>>
>>> I don't believe that you could overload those particular operators,
>>> since to my knowledge they do not exist in Python to begin with.
>>
>> It all depends on if the operators use special methods on objects:
>> http://docs.python.org/reference/datamodel.html#special-method-names
>>
>> You can overload => via object.__le__, for example.
>
> Yes, but it will be a comparison operator, and to an extent, will be
> assumed to function as one. I don't recommend abusing comparisons for
> other operations - you'll confuse people no end.
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list

Actually, the >= operator can't be spelled => anyway:

>>> 3=>4
File "<stdin>", line 1
3=>4
^
SyntaxError: invalid syntax

--
~Zahlman {:>
--
http://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Apr 19, 2012, 11:09 PM

Post #8 of 18 (994 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

alex23 <wuwei23 [at] gmail> writes:

> On Apr 20, 5:54 am, Jacob MacDonald <jaccar...@gmail.com> wrote:
>
> > On Thursday, April 19, 2012 12:28:50 PM UTC-7, dmitrey wrote:
> > > can I somehow overload operators like "=>", "->" or something like
> > > that?
> > I don't believe that you could overload those particular operators,
> > since to my knowledge they do not exist in Python to begin with.

There is no ‘=>’ operator, and no ‘->’ operator, in Python
<URL:http://docs.python.org/reference/lexical_analysis.html#operators>.
>
> It all depends on if the operators use special methods on objects:
> http://docs.python.org/reference/datamodel.html#special-method-names
>
> You can overload => via object.__le__, for example.

No, ‘<=’ is the less-than-or-equal operator. There is no ‘=>’ operator
in Python.

--
\ “I knew things were changing when my Fraternity Brothers threw |
`\ a guy out of the house for mocking me because I'm gay.” |
_o__) —postsecret.com, 2010-01-19 |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


jaccarmac at gmail

Apr 20, 2012, 8:06 AM

Post #9 of 18 (992 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On Thursday, April 19, 2012 11:09:52 PM UTC-7, Ben Finney wrote:
> alex23 <wuwei23 [at] gmail> writes:
>
> > On Apr 20, 5:54am, Jacob MacDonald <jaccar...@gmail.com> wrote:
> >
> > > On Thursday, April 19, 2012 12:28:50 PM UTC-7, dmitrey wrote:
> > > > can I somehow overload operators like "=>", "->" or something like
> > > > that?
> > > I don't believe that you could overload those particular operators,
> > > since to my knowledge they do not exist in Python to begin with.
>
> There is no => operator, and no -> operator, in Python
> <URL:http://docs.python.org/reference/lexical_analysis.html#operators>.
> >
> > It all depends on if the operators use special methods on objects:
> > http://docs.python.org/reference/datamodel.html#special-method-names
> >
> > You can overload => via object.__le__, for example.
>
> No, <= is the less-than-or-equal operator. There is no => operator
> in Python.
>
> --
> \ I knew things were changing when my Fraternity Brothers threw |
> `\ a guy out of the house for mocking me because I'm gay. |
> _o__) postsecret.com, 2010-01-19 |
> Ben Finney

Thought so.
--
http://mail.python.org/mailman/listinfo/python-list


nobody at nowhere

Apr 20, 2012, 8:50 AM

Post #10 of 18 (990 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On Thu, 19 Apr 2012 12:28:50 -0700, dmitrey wrote:

> can I somehow overload operators like "=>", "->" or something like that?
> (I'm searching for appropriate overload for logical implication "if a then
> b")

You cannot create new operators, but you can control how existing
operators work on types which you define.

IOW, you can't define "->" or "=>", but you could define ">=" or ">>".

--
http://mail.python.org/mailman/listinfo/python-list


kiuhnm03.4t.yahoo.it at mail

Apr 20, 2012, 9:45 AM

Post #11 of 18 (991 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On 4/20/2012 17:50, Nobody wrote:
> On Thu, 19 Apr 2012 12:28:50 -0700, dmitrey wrote:
>
>> can I somehow overload operators like "=>", "->" or something like that?
>> (I'm searching for appropriate overload for logical implication "if a then
>> b")
>
> You cannot create new operators, but you can control how existing
> operators work on types which you define.
>
> IOW, you can't define "->" or "=>", but you could define">=" or ">>".

You can also "overload" '<-' ;)

Kiuhnm
--
http://mail.python.org/mailman/listinfo/python-list


python.list at tim

Apr 20, 2012, 10:43 AM

Post #12 of 18 (991 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On 04/20/12 11:45, Kiuhnm wrote:
>> IOW, you can't define "->" or "=>", but you could define">=" or ">>".
>
> You can also "overload" '<-' ;)

Oooh, that's evil. Slick, but evil! :-D

-tkc


--
http://mail.python.org/mailman/listinfo/python-list


jpiitula at ling

Apr 20, 2012, 10:49 AM

Post #13 of 18 (990 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

Kiuhnm writes:
> On 4/20/2012 17:50, Nobody wrote:
> > On Thu, 19 Apr 2012 12:28:50 -0700, dmitrey wrote:
> >
> >> can I somehow overload operators like "=>", "->" or something
> >> like that? (I'm searching for appropriate overload for logical
> >> implication "if a then b")
> >
> > You cannot create new operators, but you can control how existing
> > operators work on types which you define.
> >
> > IOW, you can't define "->" or "=>", but you could define">=" or
> > ">>".
>
> You can also "overload" '<-' ;)

Huh. If you're thinking what you just made me think, maybe x -=1> y
could be made to work. Think of --> but Python doesn't have -- ...

I forget whether it is just the Java people or also the Python group
who feel very strongly that the truth values should not really be
ordered and it is something like a bug in the language that they are.

Because <= already works as a material implication for the built-in
True and False to a certain extent: not for general truth values, and
x <= y <= z does not mean x <= (y <= z), and seen as an arrow the
symbol does feel backwards, and probably other such issues.
--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

Apr 20, 2012, 10:59 AM

Post #14 of 18 (990 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

Kiuhnm wrote:

> You can also "overload" '<-' ;)

In the same spirit:

http://code.activestate.com/recipes/384122-infix-operators/


--
http://mail.python.org/mailman/listinfo/python-list


kiuhnm03.4t.yahoo.it at mail

Apr 20, 2012, 11:21 AM

Post #15 of 18 (991 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On 4/20/2012 19:49, Jussi Piitulainen wrote:
> Kiuhnm writes:
>> On 4/20/2012 17:50, Nobody wrote:
>>> On Thu, 19 Apr 2012 12:28:50 -0700, dmitrey wrote:
>>>
>>>> can I somehow overload operators like "=>", "->" or something
>>>> like that? (I'm searching for appropriate overload for logical
>>>> implication "if a then b")
>>>
>>> You cannot create new operators, but you can control how existing
>>> operators work on types which you define.
>>>
>>> IOW, you can't define "->" or "=>", but you could define">=" or
>>> ">>".
>>
>> You can also "overload" '<-' ;)
>
> Huh. If you're thinking what you just made me think, maybe x -=1> y
> could be made to work. Think of --> but Python doesn't have -- ...
>
> I forget whether it is just the Java people or also the Python group
> who feel very strongly that the truth values should not really be
> ordered and it is something like a bug in the language that they are.
>
> Because<= already works as a material implication for the built-in
> True and False to a certain extent: not for general truth values, and
> x<= y<= z does not mean x<= (y<= z), and seen as an arrow the
> symbol does feel backwards, and probably other such issues.

It's clear that Python wasn't developed with DSLs in mind. That's not a
bad thing. It's just the way it is.
Nice work with that '-=1>', by the way :)
Another option is to rely on ugly pseudo-operators:
a .imp. b
a .imp (b .imp. c)

Kiuhnm
--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Apr 20, 2012, 11:33 AM

Post #16 of 18 (990 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On Sat, Apr 21, 2012 at 4:21 AM, Kiuhnm
<kiuhnm03.4t.yahoo.it [at] mail> wrote:
> Another option is to rely on ugly pseudo-operators:
> a .imp. b
> a .imp (b .imp. c)

Or functions:

implies(a,b)
implies(a,implies(b,c))

That's the simplest option :) No hacks required, syntax is clear even
if the reader doesn't know what you're doing!

ChrisA
--
http://mail.python.org/mailman/listinfo/python-list


steveo at syslang

Apr 20, 2012, 2:34 PM

Post #17 of 18 (991 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On 4/19/2012 3:28 PM, dmitrey wrote:
> hi all,
> can I somehow overload operators like "=>", "->" or something like
> that? (I'm searching for appropriate overload for logical implication
> "if a then b")
> Thank you in advance, D.

This tickled a memory from decades back when I worked in PL/I. They have a
bool builtin function that performed bitwise boolean operations. The first two
args were bit strings. The third arg is 4 bits whose value denoted the logical
operation to be performed on the first two args on a bit by bit basis.

The meaning of the four bits are

1st bit of arg3 defines the result of bool if A = '0'B and B = '0'B
2nd bit of arg3 defines the result of bool if A = '0'B and B = '1'B
3rd bit of arg3 defines the result of bool if A = '1'B and B = '0'B
4th bit of arg3 defines the result of bool if A = '1'B and B = '1'B

bool ( A , B , '0001'B ) A AND B logical AND
bool ( A , B , '0111'B ) A OR B logical OR
bool ( A , B , '0110'B ) A XOR B exclusive-OR
bool ( A , B , '1110'B ) A NAND B NOT AND
bool ( A , B , '1000'B ) A NOR B NOT OR
bool ( A , B , '1001'B ) A IFF B equivalence
bool ( A , B , '1101'B ) A -> B implication: if A then B
bool ( A , B , '1011'B ) A <- B implication: if B then A

If A and B are just one bit (like True or False) then the operation basically
becomes logical instead of bitwise. The full explanation is over at

http://tinyurl.com/855dzjm

Also more detail is here

http://tinyurl.com/cdhwgdj

So, if you *really* wanted to, you could define a bunch of constants like
BOP_AND, BOP_OR, BOP_XOR, BOP_NAND, BOP_NOR, BOP_IFF, BOP_IMP and BOP_NIMP.

Then define a class called Bool that redefines things like __rlshift__ and
__rrshift__. That would get >>= and <<= for Implications and nodus tolens.
It's not a total solution. I can't see how you're going to get IFF, NAND and NOR.

But, maybe writing a bool function might be enough. If you implement bool in
python then you can say things like

def condition(A, B, bop):
return bool(A, B, bop)

A = something_boolish_thats_long_and_complex
B = something_boolish_thats_long_and_more_complex

bop = give_me_a_boolean_condition(*xx)

boolVal = do_on_condition() if bool(A, B, bop) else None

I think this might be useful to the right person where you have to calculate
the logical operation at run-time. Passing in a constant operator is probably
less useful.

Is this horrible?

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Apr 20, 2012, 2:52 PM

Post #18 of 18 (992 views)
Permalink
Re: can I overload operators like "=>", "->" or something like that? [In reply to]

On Sat, Apr 21, 2012 at 7:34 AM, Steven W. Orr <steveo [at] syslang> wrote:
> Then define a class called Bool that redefines things like __rlshift__ and
> __rrshift__. That would get >>= and <<= for Implications and nodus tolens.
> It's not a total solution. I can't see how you're going to get IFF, NAND and
> NOR.

IFF is simply the equality operator (cast to boolean first if necessary).

ChrisA
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.