Move __init__ from BaseSet into Set and ImmutableSet. This causes a
tiny amount of code duplication, but makes it possible to give BaseSet an __init__ that raises an exception.
This commit is contained in:
parent
e3ec296df8
commit
5033b36c44
44
Lib/sets.py
44
Lib/sets.py
|
@ -63,20 +63,12 @@ class BaseSet(object):
|
||||||
|
|
||||||
# Constructor
|
# Constructor
|
||||||
|
|
||||||
def __init__(self, seq=None):
|
def __init__(self):
|
||||||
"""Construct a set, optionally initializing it from a sequence."""
|
"""This is an abstract class."""
|
||||||
self._data = {}
|
# Don't call this from a concrete subclass!
|
||||||
if seq is not None:
|
if self.__class__ is BaseSet:
|
||||||
# I don't know a faster way to do this in pure Python.
|
raise NotImplementedError, ("BaseSet is an abstract class. "
|
||||||
# Custom code written in C only did it 65% faster,
|
"Use Set or ImmutableSet.")
|
||||||
# preallocating the dict to len(seq); without
|
|
||||||
# preallocation it was only 25% faster. So the speed of
|
|
||||||
# this Python code is respectable. Just copying True into
|
|
||||||
# a local variable is responsible for a 7-8% speedup.
|
|
||||||
data = self._data
|
|
||||||
value = True
|
|
||||||
for key in seq:
|
|
||||||
data[key] = value
|
|
||||||
|
|
||||||
# Standard protocols: __len__, __repr__, __str__, __iter__
|
# Standard protocols: __len__, __repr__, __str__, __iter__
|
||||||
|
|
||||||
|
@ -289,9 +281,19 @@ class ImmutableSet(BaseSet):
|
||||||
|
|
||||||
def __init__(self, seq):
|
def __init__(self, seq):
|
||||||
"""Construct an immutable set from a sequence."""
|
"""Construct an immutable set from a sequence."""
|
||||||
# Override the constructor to make 'seq' a required argument
|
|
||||||
BaseSet.__init__(self, seq)
|
|
||||||
self._hashcode = None
|
self._hashcode = None
|
||||||
|
self._data = data = {}
|
||||||
|
# I don't know a faster way to do this in pure Python.
|
||||||
|
# Custom code written in C only did it 65% faster,
|
||||||
|
# preallocating the dict to len(seq); without
|
||||||
|
# preallocation it was only 25% faster. So the speed of
|
||||||
|
# this Python code is respectable. Just copying True into
|
||||||
|
# a local variable is responsible for a 7-8% speedup.
|
||||||
|
value = True
|
||||||
|
# XXX Should this perhaps look for _as_immutable?
|
||||||
|
# XXX If so, should use self.update(seq).
|
||||||
|
for key in seq:
|
||||||
|
data[key] = value
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
if self._hashcode is None:
|
if self._hashcode is None:
|
||||||
|
@ -306,6 +308,16 @@ class Set(BaseSet):
|
||||||
|
|
||||||
# BaseSet + operations requiring mutability; no hashing
|
# BaseSet + operations requiring mutability; no hashing
|
||||||
|
|
||||||
|
def __init__(self, seq=None):
|
||||||
|
"""Construct an immutable set from a sequence."""
|
||||||
|
self._data = data = {}
|
||||||
|
if seq is not None:
|
||||||
|
value = True
|
||||||
|
# XXX Should this perhaps look for _as_immutable?
|
||||||
|
# XXX If so, should use self.update(seq).
|
||||||
|
for key in seq:
|
||||||
|
data[key] = value
|
||||||
|
|
||||||
# In-place union, intersection, differences
|
# In-place union, intersection, differences
|
||||||
|
|
||||||
def union_update(self, other):
|
def union_update(self, other):
|
||||||
|
|
Loading…
Reference in New Issue