473,803 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Was: Is this an Bug in python 2.3?? Reflective relational operators

Dear Jeff,

Thanks for your reply. I see that the PEP and the current docs
specify that __ge__ and __le__ are their own reflection.

Given that PEP 207 proposes rich comparison to handle cases
(brought upon by NumPy) where the objects returned should not be
assumed to be boolean, what is the argument for automatic reflection?

Automatic reflection prevents the resulting objects from being
dependant on the ordering of the arguments to __ge__ and __le__. In
our application, this ordering is important.

Can anyone think of a way to get around automatic reflection of
relational operators?

TIA,
Balaji.
Jul 18 '05 #1
9 1502
ba****@email.ar izona.edu (Balaji) writes on 12 Jun 2004 16:38:07 -0700:
Thanks for your reply. I see that the PEP and the current docs
specify that __ge__ and __le__ are their own reflection.
...
Automatic reflection prevents the resulting objects from being
dependant on the ordering of the arguments to __ge__ and __le__. In
our application, this ordering is important.
? The order of "__ge__" and "__le__" operands is always important -- not
only in your application...

I read the documentation as "__le__" is the reflection of "__ge__" and vice
versa. This seems to be senseful.
Can anyone think of a way to get around automatic reflection of
relational operators?


Define the respected operators for both operands. Then, there is no
need for reflection.

Dieter
Jul 18 '05 #2
Hello Everybody...

Let me try to explain what do I mean by "ordering importance".

In our application a expression 100>=x reads as 100 is an upper bound
on x. Mathematically it is same as saying x<=100 which also bounds x
by 100. But in modelling, the user wants to preserve the order in
which he has written the expression.

If the left side of an operand is an immutable type then this ordering
is no longer preserved, even if both the methods are implemented(i.e
__le__ and __ge__).

__radd__ allows you to have an immutable type on the left side. An
__rge__ method would provide the same functionality for relational
operators.

Is there a way to obtain this behavior with current syntax?

Regards

Balaji
Jul 18 '05 #3
Hello Everybody...

Here is a code (tested)

class Test:

def __le__(self,oth er):
print " <= "
def __ge__(self,oth er):
print " >= "
def __add__(self,ot her):
print " + "
def __radd__(self,o ther):
print " r+ "
Suppose s is an instance of Test.

s=Test()

s>=100

prints >=

but 100>=s

prints <=

The point here is mathematically it is right but not modelling wise.

By modelling I mean, the user wants to preserve the order in which he
typed the
expression.

Is there a way to obtain the radd behavior in __ge__ and __le__ with
current syntax..

Regards

Balaji
Jul 18 '05 #4
Balaji wrote:
Hello Everybody...

Let me try to explain what do I mean by "ordering importance".

In our application a expression 100>=x reads as 100 is an upper bound
on x. Mathematically it is same as saying x<=100 which also bounds x
by 100. But in modelling, the user wants to preserve the order in
which he has written the expression.

If the left side of an operand is an immutable type then this ordering
is no longer preserved, even if both the methods are implemented(i.e
__le__ and __ge__).

No, as far as I understand the doc, if the left side does not support
comparing to your instance on the left then your instance's reversed
operator method is called.
__radd__ allows you to have an immutable type on the left side. An
__rge__ method would provide the same functionality for relational
operators.

Is there a way to obtain this behavior with current syntax?

I far as I know, there is no __rge__ and no possible way to detect if
ian op was called from either syntax.
The comparison operators should be used in cases where the "reflective "
behaviour is sound. I beleive they should not be used for other things
than comparing instances for witch there is a complete order (many
others will disagree).

--
Grégoire Dooms
Jul 18 '05 #5
Leo
> The comparison operators should be used in cases where the "reflective "
behaviour is sound. I beleive they should not be used for other things
than comparing instances for witch there is a complete order (many
others will disagree).


I don't immediately see why we should restrict the usage of comparison
operators to that case, but that doesn't matter. What (maybe) matters
is: If we were to ask (PEP) for an __rge__ with a default
implementation that calls __le__ with the parameters inverted, would
that break anything?

I can think of other applications where this would be useful. For
example: generating MathML from python expressions. Or looking at it
another way, wouldn't it improve language consistency to add an
__rge__ method?

Unfortunately in PEP 207 the issue of reflexivity is raised and
decided, but not justified. Maybe at the time no one had an example
where this feature would be useful? Would it be too hard to change?

Thx,
Leo.
Jul 18 '05 #6
ba****@email.ar izona.edu (Balaji) wrote in message news:<49******* *************** ****@posting.go ogle.com>...
Hello Everybody...

[against Python's automatic reflection of comparison operators]

By modelling I mean, the user wants to preserve the order in which he
typed the expression.


Why not preserve the original text of the expression?

What exactly are you trying to do?
Jul 18 '05 #7
ba****@email.ar izona.edu (Balaji) writes on 14 Jun 2004 11:37:42 -0700:
...
In our application a expression 100>=x reads as 100 is an upper bound
on x. Mathematically it is same as saying x<=100 which also bounds x
by 100. But in modelling, the user wants to preserve the order in
which he has written the expression.


Python's "rich comparison" methods have been defined for
comparisons and not for modelling. For any senseful definition
of ">=" and "<=", "x >= y" is equivalent to "y <= x".

As you seem to be interested in modeling and not in comparisons,
you must use your mapping of abstract syntax to objects
and can not use Python's default one.

The "ast" modul contains necessary prerequisites to implement
such a mapping. Recently, I found an example in Zope's
"RestrictedPyth on".
Dieter
Jul 18 '05 #8
Dear Dan,

What I'm trying to do in my application is preserve the order in which
the user wrote an expression.

If a user writes e1=100>=x, python treats it as e1= x<=100. Both are
same mathematically but semantically they are not. If a user prints
this expression he will get e1= x<=100, which is different than
e1=100<=x.

In my application both the <= and >= have totally different meaning.
They are not just used for comparisson.

Regards

Balaji
Jul 18 '05 #9
Dear Dan,

What I'm trying to do in my application is preserve the order in which
the user wrote an expression.

If a user writes e1=100>=x, python treats it as e1= x<=100. Both are
same mathematically but semantically they are not. If a user prints
this expression he will get e1= x<=100, which is different than
e1=100<=x.

In my application both the <= and >= have totally different meaning.
They are not just used for comparisson.

Regards

Balaji
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
2999
by: Xah Lee | last post by:
is it possible in Python to create a function that maintains a variable value? something like this: globe=0; def myFun(): globe=globe+1 return globe
59
3955
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a candidate for Python 3000 yet? Chris
27
3146
by: glen herrmannsfeldt | last post by:
The subject recently came up in comp.compilers, though I believe that I asked here before. If you use relational operators, other than == and !=, on pointers to different objects, is there any requirement on the result? #include <stdio.h> int main() {
25
1728
by: noe | last post by:
Hello, I'm writing a file system filter driver and I've found in an example this sentence: if (VALID_FAST_IO_DISPATCH_HANDLER( fastIoDispatch, FastIoRead )) { return (fastIoDispatch->FastIoRead)( FileObject, FileOffset, Length, Wait,
49
14915
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
66
7094
by: mensanator | last post by:
Probably just me. I've only been using Access and SQL Server for 12 years, so I'm sure my opinions don't count for anything. I was, nevertheless, looking forward to Sqlite3. And now that gmpy has been upgraded, I can go ahead and install Python 2.5. So I open the manual to Section 13.13 where I find the first example of how to use Sqlite3:
14
2573
by: AliceB.Toklas | last post by:
In my Absolute Beginner's Guide to C book by Greg Perry where it is instruction how relational operators work it gives the following example: int i = 5; so the following statement is true: int i == 1
34
3697
by: Anthony Irwin | last post by:
Hi All, I am currently trying to decide between using python or java and have a few quick questions about python that you may be able to help with. #1 Does python have something like javas .jar packages. A jar file contains all the program files and you can execute the program with java -jar program.jar I am sort of hoping python has something like this because I feel it
1
1313
by: jsparks57 | last post by:
I want to cin two characters for any of the six relational operators. '>=' no problem or '!=' no problem. The problem is for reading single character '<' and '>'. My code wants to read two characters but what works for a second character for a single character relational operator. The blank character ' ' is not accepted. Maybe I should define '<' as '<,' or '<.' to get a simulated second character for cin to accept?
0
9566
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.