파이썬 문법이 함수 밖에서 return 문을 허용한다는 것을 알았지 만, 나는 정말로 이해하지 못합니다. 이유는 무엇입니까? 나는 문법을 지정할 수 있다고 믿는다. 그래서 허용되지 않는다. 파이썬 문법의 끝 "함수 밖의 반환"
이
이 있습니다 파이썬 문법의 일부입니다 : 또한single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
return_stmt: 'return' [testlist]
인터프리터 보고서
이 구문과 같은 오류 (함수 외부 '리턴'),하지만이 아닌 경우 어떻게이를 감지 파서 수 있습니다 문법에 명시되어 있습니까?
나는 들어 본 적이 없지만, 예제에서 함수 밖의 return 문이 어디에 있습니까? – Julien
글쎄,이 문법에서 우리는 : single_input => simple_stmt => small_stmt NEWLINE => flow_stmt NEWLINE => return_stmt NEWLINE => 'NEWLINE'을 반환합니다. single_input은 문법의 시작 심볼이므로 (파이썬 문서에서 언급했듯이) 우리는 이것을 가지고있다. –