预计阅读本页时间:-
Test Your Knowledge: Quiz
- What is the output of the following code, and why?
>>> X = 'Spam'
>>> def func():
... print(X)
...
>>> func() - What is the output of this code, and why?
>>> X = 'Spam'
>>> def func():
... X = 'NI!'
...
>>> func()
>>> print(X) - What does this code print, and why?
>>> X = 'Spam'
>>> def func():
... X = 'NI'
... print(X)
...
>>> func()
>>> print(X) - What output does this code produce? Why?
>>> X = 'Spam'
>>> def func():
... global X
... X = 'NI'
...
>>> func()
>>> print(X) - What about this code—what’s the output, and why?
>>> X = 'Spam'
>>> def func():
... X = 'NI'
... def nested():
... print(X)
... nested()
...
>>> func()
>>> X - How about this example: what is its output in Python 3.0, and why?
>>> def func():
... X = 'NI'
... def nested():
... nonlocal X
... X = 'Spam'
... nested()
... print(X)
...
>>> func() - Name three or more ways to retain state information in a Python function.