[Python-bugs-list] [ python-Bugs-672098 ] Three __contains__ implementations

SourceForge.net noreply@sourceforge.net
Wed, 29 Jan 2003 17:03:36 -0800


Bugs item #672098, was opened at 2003-01-21 17:18
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=672098&group_id=5470

Category: Python Library
>Group: Python 2.3
>Status: Closed
Resolution: Accepted
Priority: 5
Submitted By: Jp Calderone (kuran)
Assigned to: Nobody/Anonymous (nobody)
Summary: Three __contains__ implementations

Initial Comment:

  Several classes in the stdlib implement dict-like
interfaces, but do not provide __contains__
definitions.  This is mentioned in PEP290 as a
"contra-indication" for updating code to use "k in d".
 The attached patch adds __contains__ implementations
for these stdlib classes.  As far as I can tell, this
brings all the dict-like classes in the stdlib "up to
date", so perhaps PEP290 should be updated as well.


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-01-29 20:03

Message:
Logged In: YES 
user_id=80475

Applied as:

lib-tk/Canvas.py 1.18
plat-riscos/riscosenv 1.6
xml/sax/xmlreader.py 1.17


----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-01-24 22:30

Message:
Logged In: YES 
user_id=80475

This patch is fine as it stands.  Brett's point is well taken, 
but using 'in' should never result in a reduced 
performance. IMO, the additional layer of indirection isn't 
worth it.

PEP 290 needs to stay "as is" because it still applies to 
home-grown and third-party modules.



----------------------------------------------------------------------

Comment By: Brett Cannon (bcannon)
Date: 2003-01-21 23:51

Message:
Logged In: YES 
user_id=357491

The code itself looks fine, but I would suggest that instead of copying code from the methods that provide the same that you change them so that the old method calls ``__contains__`` and that ``__contains__`` now takes over as the primary method.  So instead of::

     def has_key(self, name):
         return self._attrs.has_key(name)
 
    def __contains__(self, name):
        return self._attrs.has_key(name)

do::

   def has_key(self, name):
         return self.__contains__(name)

   def __contains__(self, name):
         return self._attrs.has_key(name)


Doing it this way minimizes code duplication along with the possibility of someone changing one method and not the other.

And as for usefulness of these additions, I can't comment since I use none of the modules patched, but I see no harm in adding the functionality.  So  +0 from me on applying the patch once the above changes are done.

----------------------------------------------------------------------

Comment By: Jp Calderone (kuran)
Date: 2003-01-21 21:16

Message:
Logged In: YES 
user_id=366566

Either mozilla or sf is screwing with me.  *grumble* 
There's no way I selected bisect.py for attachment.  Proper
file now attached.


----------------------------------------------------------------------

Comment By: Brett Cannon (bcannon)
Date: 2003-01-21 19:44

Message:
Logged In: YES 
user_id=357491

The attached file has no ``__contains__`` definitions for anything.  Did you attach the correct file?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=672098&group_id=5470