[issue45313] Counter.elements() wrong error message

2021-09-28 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

The elements() method is working correctly:

>>> from collections import Counter
>>> aCounter = Counter('abracadabra')
>>> list(aCounter.elements())
['a', 'a', 'a', 'a', 'a', 'b', 'b', 'r', 'r', 'c', 'd']

No arguments should be passed into elements, so this code is incorrect:

aCounter.elements(1)

You may have expected that the error message would say:

TypeError: Counter.elements() takes 0 positional argument but 1 was given

Instead, you observed the counts were one higher than you expected.

This is due to how Python implements object oriented programming.

The call:

aCounter.elements(1)

is effectively translated to:

Counter.elements(aCounter, 1)

which does in fact have two arguments.

No one really likes the error message, but it has been deemed hard to fix and 
we just live with it.

Note, this isn't specific to Counter.  All pure python classes work this way:

>>> class A:
def m(self, x):
pass

>>> a = A()
>>> a.m(10, 20)
Traceback (most recent call last):
  File "", line 1, in 
a.m(10, 20)
TypeError: m() takes 2 positional arguments but 3 were given

--
nosy: +rhettinger
resolution:  -> wont fix
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45313] Counter.elements() wrong error message

2021-09-28 Thread Eric V. Smith


Eric V. Smith  added the comment:

When reporting a bug, please give a full example that we can run. I assume 
aCounter is of type collections.Counter?

Assuming so, the "1 positional argument" is "self". The second argument is the 
number 1, which is an error. So the error is correct.

--
nosy: +eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45313] Counter.elements() wrong error message

2021-09-28 Thread bytebites


New submission from bytebites :

aCounter.elements(1) gives wrong error message:
TypeError: Counter.elements() takes 1 positional argument but 2 were given

--
messages: 402795
nosy: bytebites
priority: normal
severity: normal
status: open
title: Counter.elements() wrong error message
type: behavior
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com