diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-11-27 17:02:56 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-11-27 17:02:56 (GMT) |
commit | 20994f1e27c38973f1854dbdcf9b29fe8e3cd6be (patch) | |
tree | 95ab352827a4a6bf7669fdcc611c6e785553acc3 /Lib/collections.py | |
parent | 6ac2b4db9463b366244b34cea6959426a645c1a2 (diff) | |
download | cpython-20994f1e27c38973f1854dbdcf9b29fe8e3cd6be.zip cpython-20994f1e27c38973f1854dbdcf9b29fe8e3cd6be.tar.gz cpython-20994f1e27c38973f1854dbdcf9b29fe8e3cd6be.tar.bz2 |
Issue #22609: Constructors and update methods of mapping classes in the
collections module now accept the self keyword argument.
Diffstat (limited to 'Lib/collections.py')
-rw-r--r-- | Lib/collections.py | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index 8831f0b..1dcd233 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -35,12 +35,17 @@ class OrderedDict(dict): # The sentinel element never gets deleted (this simplifies the algorithm). # Each link is stored as a list of length three: [PREV, NEXT, KEY]. - def __init__(self, *args, **kwds): + def __init__(*args, **kwds): '''Initialize an ordered dictionary. The signature is the same as regular dictionaries, but keyword arguments are not recommended because their insertion order is arbitrary. ''' + if not args: + raise TypeError("descriptor '__init__' of 'OrderedDict' object " + "needs an argument") + self = args[0] + args = args[1:] if len(args) > 1: raise TypeError('expected at most 1 arguments, got %d' % len(args)) try: @@ -438,7 +443,7 @@ class Counter(dict): # http://code.activestate.com/recipes/259174/ # Knuth, TAOCP Vol. II section 4.6.3 - def __init__(self, iterable=None, **kwds): + def __init__(*args, **kwds): '''Create a new, empty Counter object. And if given, count elements from an input iterable. Or, initialize the count from another mapping of elements to their counts. @@ -449,8 +454,15 @@ class Counter(dict): >>> c = Counter(a=4, b=2) # a new counter from keyword args ''' + if not args: + raise TypeError("descriptor '__init__' of 'Counter' object " + "needs an argument") + self = args[0] + args = args[1:] + if len(args) > 1: + raise TypeError('expected at most 1 arguments, got %d' % len(args)) super(Counter, self).__init__() - self.update(iterable, **kwds) + self.update(*args, **kwds) def __missing__(self, key): 'The count of elements not in the Counter is zero.' @@ -501,7 +513,7 @@ class Counter(dict): raise NotImplementedError( 'Counter.fromkeys() is undefined. Use Counter(iterable) instead.') - def update(self, iterable=None, **kwds): + def update(*args, **kwds): '''Like dict.update() but add counts instead of replacing them. Source can be an iterable, a dictionary, or another Counter instance. @@ -521,6 +533,14 @@ class Counter(dict): # contexts. Instead, we implement straight-addition. Both the inputs # and outputs are allowed to contain zero and negative counts. + if not args: + raise TypeError("descriptor 'update' of 'Counter' object " + "needs an argument") + self = args[0] + args = args[1:] + if len(args) > 1: + raise TypeError('expected at most 1 arguments, got %d' % len(args)) + iterable = args[0] if args else None if iterable is not None: if isinstance(iterable, Mapping): if self: @@ -536,7 +556,7 @@ class Counter(dict): if kwds: self.update(kwds) - def subtract(self, iterable=None, **kwds): + def subtract(*args, **kwds): '''Like dict.update() but subtracts counts instead of replacing them. Counts can be reduced below zero. Both the inputs and outputs are allowed to contain zero and negative counts. @@ -552,6 +572,14 @@ class Counter(dict): -1 ''' + if not args: + raise TypeError("descriptor 'subtract' of 'Counter' object " + "needs an argument") + self = args[0] + args = args[1:] + if len(args) > 1: + raise TypeError('expected at most 1 arguments, got %d' % len(args)) + iterable = args[0] if args else None if iterable is not None: self_get = self.get if isinstance(iterable, Mapping): |