In python3, when I run
>>> exec("","","")
TypeError: exec() arg 2 must be a dict, not str
>>> exec( "print('Hello')", print("World"), print("!") )
World
!
Hello
>>> type(print("World"))
World
<class 'NoneType'>
I mean in the Python3, the arg2 of exec() expects a dict, but we can still put a print() function which is not a dict. why?
2 Answers 2
Simple!
It's acceptable because its value is None (it can accept None or a dict), which is the default value for the argument.
In a example, a call such as:
exec("print('Hello')")
Is the same as:
exec("print('Hello')", None, None)
glglgl
91.5k13 gold badges157 silver badges230 bronze badges
answered May 30, 2013 at 19:58
Fernando Jorge Mota
1,5641 gold badge10 silver badges13 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
print returns None, which is a valid argument for an optional parameter.
answered May 30, 2013 at 19:59
user1726343
Comments
lang-py