호출하지 않습니다. 완료시 수행되는 작업은 실제로 (() 연산자를 사용하여) 호출 할 때만 관련됩니다. return 문을 정의하지 않으면 None을 반환합니다. 여기에 설명 된대로 : Python -- return, return None, and no return at all.
제공된 명령을 실행할 때 함수 func을 호출하자 마자 무언가 정의되지 않았으므로 곧 실행됩니다. pycharm이 잘못된 강조 표시를하고 있음을 두려워합니다. d 및 d2는 호출 가능하지만 d3은 호출 할 수 없습니다. d3을 지정할 때 func를 호출하기 때문에 여기서 오류가 발생하고 d3은 존재하지 않습니다.
Python 2.7.12 (default, Oct 10 2016, 12:50:22)
[GCC 5.4.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
dlopen("/usr/lib/python2.7/lib-dynload/readline.dll", 2);
import readline # dynamically loaded from /usr/lib/python2.7/lib-dynload/readline.dll
>>>
>>> def func():
... something
...
>>> d = { 'func': func }
>>> d['func']()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in func
NameError: global name 'something' is not defined
>>>
>>> d2 = { 'type': { 'func': func } }
>>> d2['type']['func']()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in func
NameError: global name 'something' is not defined
>>>
>>> d3 = { 'type': { 'func': func() } }
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in func
NameError: global name 'something' is not defined
>>> d3['type']['func']()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'd3' is not defined
출처
2016-12-25 11:08:29
mic
'd2 [ 'func']'와'd3 [ 'func']'모두 오류를 발생시켜야합니다. 존재하지 않는 키 ('func')를 잡는 중입니다. 당신은'd2 [ 'type'] [ 'func']'와'd3 [ 'type'] [ 'func']'를 의미합니까? – Abdou
마지막 두 예제가 작동하지 않아야합니다. ' "func"'는 즉각적인 키가 아닙니다. dict() –
죄송합니다. –