Skip to content

hook method for 'is' operator #36120

Closed
Closed

Description

@mathematician
BPO 519227
Nosy @tim-one, @nascheme

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2013-05-07.00:33:17.687>
created_at = <Date 2002-02-18.17:12:12.000>
labels = ['interpreter-core', 'type-feature']
title = "hook method for 'is' operator"
updated_at = <Date 2013-05-07.00:33:17.687>
user = 'https://bugs.python.org/mathematician'

bugs.python.org fields:

activity = <Date 2013-05-07.00:33:17.687>
actor = 'lesmana'
assignee = 'none'
closed = True
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2002-02-18.17:12:12.000>
creator = 'mathematician'
dependencies = []
files = []
hgrepos = []
issue_num = 519227
keywords = []
message_count = 7.0
messages = ['53484', '53485', '53486', '53487', '53488', '53489', '53490']
nosy_count = 4.0
nosy_names = ['tim.peters', 'nascheme', 'mathematician', 'lesmana']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue519227'
versions = ['Python 3.3']

Activity

  1. mathematician commented on Feb 18, 2002

    mathematicianmannequin
    MannequinAuthor

    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'

  2. added
    interpreter-core
    type-feature
    on Feb 18, 2002
  3. added
    interpreter-core
    type-feature
    on Feb 18, 2002
  4. nascheme commented on Feb 18, 2002

    @nascheme
    Member

    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.

  5. mathematician commented on Feb 18, 2002

    mathematicianmannequin
    MannequinAuthor

    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...

  6. tim-one commented on Feb 19, 2002

    @tim-one
    Member

    Logged In: YES
    user_id=31435

    I'm afraid I agree with Neil that this would be a
    disaster. There's code that absolutely depends on "is"
    meaning object identity. One example (you'll find others
    if you just look for them): _deepcopy_tuple() in the
    standard copy.py relies on it, in its second loop. If the
    operator ever "lied" about object identity, the semantics
    of deep copies could break in amazing ways. There's lots
    of "foundational" code in a similar boat (e.g., my own
    Cyclops.py replies on current "is" semantics all over the
    place, and that's an important example because it's not in
    the standard distribution: we have no way to locate, let
    alone repair, all the code that would break).

    If you want to pursue this, then because it's not backward
    compatible, it will require a PEP to propose the change and
    introduce a corresponding __future__ statement.

    The other thing you'll get resistance on is that "is" is
    dirt cheap today, and some code relies on that too. If it
    has to look for an object override, what's currently an
    exceptionally fast implementation:

    case PyCmp_IS:
    case PyCmp_IS_NOT:
    	res = (v == w);
    	if (op == (int) PyCmp_IS_NOT)
    		res = !res;
    	break;
    

    will at least have to do new indirection dances too through
    the type objects (to see first whether either operand
    overrides "is").

  7. mathematician commented on Feb 19, 2002

    mathematicianmannequin
    MannequinAuthor

    Logged In: YES
    user_id=118203

    what about:

    x is y -> id(x)==id(y) or x.__is__(y)

    than old code would not break & one could use is for more
    than just object identity equivalence. Of course if the
    two operands are the same object, then it always returns
    true.

    I would rather see

    if dbrow is empty:
    	# do something

    than

    if dbrow.isEmpty():
    	# do something

    which is like java's string equivalency test
    strvar.isequal("to another string").

    This way an object could 'be' anything :) Hey, well maybe
    for Python 3000. If so, I also recommend adding an
    operator called 'is a' which is equivalent to isinstance()
    in current python.

    if d is a dict:
    	# do something
  8. tim-one commented on Feb 20, 2002

    @tim-one
    Member

    Logged In: YES
    user_id=31435

    Unless I'm missing something intended,

    x is y -> id(x)==id(y) or x.__is__(y)

    is true whenever "x is y" is true today, but may be true
    even in cases where "x is y" is false today. If so, it's
    not backward compatible, and general code relying on
    current semantics would still break. For example, any kind
    of general code that's crawling over an object graph needs
    to know whether it's seen an object before, current "is"
    can and is used to answer that question precisely, and it's
    as bad to tell it that two distinct objects are identical
    as it is to tell it that two identical objects are
    distinct. The standard copy.deepcopy() is one example
    of "general code that's crawling over an object graph".

    OO languages with object identity really need a way to ask
    about object identity, and "is" has always been that way in
    Python (btw, "is" existed long before "id()" was
    introduced). For that reason, if you write a PEP, I think
    you'd get farther by leaving "is" alone and proposing
    another spelling instead.

  9. nascheme commented on May 23, 2002

    @nascheme
    Member

    Logged In: YES
    user_id=35752

    __eql__? :-P

  10. transferred this issue fromon Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-coretype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions