[Python-bugs-list] [ python-Feature Requests-519227 ] hook method for 'is' operator
noreply@sourceforge.net
noreply@sourceforge.net
Mon, 18 Feb 2002 09:50:03 -0800
Feature Requests item #519227, was opened at 2002-02-18 09:12
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=519227&group_id=5470
Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Dan Parisien (mathematician)
Assigned to: Nobody/Anonymous (nobody)
Summary: hook method for 'is' operator
Initial Comment:
Being able to overload the 'is' operator would lead
to nicer more readable code:
# constant
Open = ("OPEN",)
# dummy class for my example
class File:
id = 0
def __init__(self, file=None):
if file is not None:
self.open(file)
# overload 'is' operator
def __is__(self, other):
if id(self)==id(other): # default
return 1
elif other==("OPEN",) and self.id!=0:
return 1
return 0
def open(self, file):
self.id = open(file).fileno
f = File("myfile.txt")
if f is Open:
print "File is open!"
else:
print "File is not open"
'is not' could just test __is__ and return 'not
retval'
----------------------------------------------------------------------
>Comment By: Dan Parisien (mathematician)
Date: 2002-02-18 09:50
Message:
Logged In: YES
user_id=118203
You can say the same for all the operators in python. The
default behavior would be object identity, but:
x is y
is the same as doing
id(x)==id(y)
So the 'is' operator is actually superfluous except for
its readability value.
Your comment seems to me like a knee jerk resistance to
change. Now if you were to tell me that it would make
python drastically slower or that it would be difficult to
implement, then you would have a good point...
----------------------------------------------------------------------
Comment By: Neil Schemenauer (nascheme)
Date: 2002-02-18 09:30
Message:
Logged In: YES
user_id=35752
The "is" operator has well defined semantics. It compares
object identity. Allowing it to be redefined would a terrible
idea, IMHO.
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=519227&group_id=5470