May i customize basic operator (such as 1==3)?

kanchy kang zzhikang at hotmail.com
Wed Feb 22 01:45:41 EST 2006


__eq__ method can resolve this problem only for class object.
what can i do in the following case?

a = somefuction(...) #a is stringType

a == "1234"?

my simple requirement is:
in some validation statments, such as, failUnless(a == "1234")
if the result is true, it's OK. otherwise, it prints a automatically.

also, i know we can implement failUnless(...) to satisfy this,
failUnless(a == b, a, b)

but i think, if we can override "==" for any type, it will make things 
conveniently...

regards!


>From: Casey Hawthorne <caseyhHAMMER_TIME at istar.ca>
>To: python-list at python.org
>Subject: Re: May i customize basic operator (such as 1==3)?
>Date: Wed, 22 Feb 2006 04:06:34 GMT
>
>I believe you are asking for a side effect from the "==" operator.
>
>Add print statements to the __eq__ method.
>
>"kanchy kang" <zzhikang at hotmail.com> wrote:
>
> >Hi,all
> >as we know, we can override the operator of one object(for example 
>__eq__).
> >my question is, how to override the basic operator?
> >for example,
> >
> >for any object comparison operator(including litterals),
> >for example,
> >a = "123"
> >b = "321"
> >
> >the boolean equation a == b,
> >i need override "==" operator like this:
> >first display a and b
> >
> >then return real boolean result (a == b).
> >
> >thanks!
> >
> >
> >-----------------------------------------------
> >Best regards,
> >kangzz
> >
> >mailto:zzhikang at hotmail.com
> >Tel : 021-65407754
> >MP: 13916928084
> >
> >_________________________________________________________________
> >Express yourself instantly with MSN Messenger! Download today - it's 
>FREE!
> >http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
>--
>Regards,
>Casey
>--
>http://mail.python.org/mailman/listinfo/python-list

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/




More information about the Python-list mailing list