-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In itertools.chain.from_iterable() there is no cls argument #62501
Comments
http://docs.python.org/2/library/itertools.html#itertools.chain.from_iterable >>> class A:
... @classmethod
... def from_iterable(iterables):
... for it in iterables:
... for element in it:
... yield element
...
>>> A.from_iterable(['ABC', 'DEF'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: from_iterable() takes 1 positional argument but 2 were given
>>> |
It is implemented as a classmethod, but the "equivalent" code doesn't need to be part of the class all. I'm not sure what should be done here (say @staticmethod? Leave the decorator off?). We should probably see what Raymond thinks. I lean toward the latter, that's the way it is in the python2 docs, and it doesn't seem to have caused any confusion. |
The 2.7 doc says 'Roughly equivalent to' rather than 'Equivalent to'. |
Perhaps it should be staticmethod, not classmethod. |
It should be a classmethod. >>> import itertools
>>> class C(itertools.chain): pass
...
>>> type(C.from_iterable(['ab', 'cd']))
<class '__main__.C'> The patch LGTM. |
+1 |
>>> import itertools
>>>
>>> class A(itertools.chain):
... def from_iter(arg):
... return A(iter(arg))
...
>>> class B(A):
... pass
...
>>> B('a', 'b')
<__main__.B object at 0x7f40116d7730>
>>> B.from_iter(['a', 'b'])
<__main__.A object at 0x7f40116d7780>
>>> it should be B |
changed iter(arg) to *arg >>> import itertools
>>>
>>> class A(itertools.chain):
... @classmethod
... def from_iter(cls, arg):
... return cls(*arg)
...
>>> class B(A):
... pass
...
>>> B('ab', 'cd')
<__main__.B object at 0x7fc280e93cd0>
>>> b = B.from_iter(['ab', 'cd'])
>>> b
<__main__.B object at 0x7fc280e93d20>
>>> next(b)
'a'
>>> next(b)
'b'
>>> next(b)
'c'
>>> next(b)
'd'
>>> next(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> |
My counter proposal bpo-18752 is that chain.from_iterable become a deprecated alias for a new function, chain_iterable. With '@classmethod' removed, the current Python equivalent would work for chain_iterable. |
New changeset 29fa1f418796 by Raymond Hettinger in branch '3.3': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: