A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2005-March/052265.html below:

[Python-Dev] Faster Set.discard() method?

[Python-Dev] Faster Set.discard() method? [Python-Dev] Faster Set.discard() method?Andrew McNamara andrewm at object-craft.com.au
Fri Mar 18 06:11:53 CET 2005
To avoid the exception in the discard method, it could be implemented as:

    def discard(self, element):
        """Remove an element from a set if it is a member.

        If the element is not a member, do nothing.
        """
        try:
            self._data.pop(element, None)
        except TypeError:
            transform = getattr(element, "__as_temporarily_immutable__", None)
            if transform is None:
                raise # re-raise the TypeError exception we caught
            del self._data[transform()]

Currently, it's implemented as the much clearer:

        try:
            self.remove(element)
        except KeyError:
            pass

But the dict.pop method is about 12 times faster. Is this worth doing?

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4