In python3, when I try to execute the following lines:
$ python3
Python 3.3.5 (default, Mar 18 2014, 02:00:02)
[GCC 4.2.1 20070831 patched [FreeBSD]] on freebsd9
Type "help", "copyright", "credits" or "license" for more information.
>>> def fail(arg):
... raise Exception
...
>>> def fail_type(arg):
... raise TypeError
...
>>> identity = lambda x: x
>>> m = [0,1]
>>> print("{:8.2f}{:8.2f}".format(*map(identity, m)))
0.00 1.00
>>> print("{:8.2f}{:8.2f}".format(*map(fail, m)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fail
Exception
>>> print("{:8.2f}{:8.2f}".format(*map(fail_type, m)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: format() argument after * must be a sequence, not map
>>> list(map(identity, m))
[0, 1]
>>> list(map(fail, m))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fail
Exception
>>> list(map(fail_type, m))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fail_type
TypeError
>>>
Why does python throw that in my opinion highly misleading TypeError: format() argument after * must be a sequence, not map
for this builtin function? It makes it even more confusing to see that an explicit raise
for other Exception
s is caught an passed through. Is there an assumption in *
error handling that a TypeError
must be due to the argumentlist not being a sequence?
Python's map() is a built-in function that allows you to process and transform all the items in an iterable without using an explicit for loop, a technique commonly known as mapping. map() is useful when you need to apply a transformation function to each item in an iterable and transform them into a new iterable.
map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.)
Python map() function is used to apply a function on all the elements of specified iterable and return map object. Python map object is an iterator, so we can iterate over its elements. We can also convert map object to sequence objects such as list, tuple etc. using their factory functions.
Creating a ChainMap We create two dictionaries and club them using the ChainMap method from the collections library. Then we print the keys and values of the result of the combination of the dictionaries. If there are duplicate keys, then only the value from the first key is preserved.
The TypeError
exception match occurs in ext_do_call
(Python/ceval.c, line 4507), which is called to handle the opcodes CALL_FUNCTION_VAR
, CALL_FUNCTION_KW
, and CALL_FUNCTION_VAR_KW
. It's assuming PySequence_Tuple
raised the TypeError
because the argument wasn't iterable, as is raised by PyObject_GetIter
.
There is an opened Python bug in "patch review" stage for 3 years: Function calls taking a generator as star argument can mask TypeErrors in the generator
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With