파이썬 키워드 인수
문자열이 Python에서 유효한 키워드인지 확인하는 방법은 무엇입니까?
키워드란?
키워드는 통역사에게 특별한 의미를 전달하는 언어의 "예약어"입니다. 명령 또는 매개변수일 수 있습니다. 프로그램에서 변수 이름, 함수 이름 또는 기타 식별자로 사용할 수 없는 단어입니다.
파이썬 키워드는 무엇입니까?
파이썬 키워드는 특별한 의미를 전달하는 단어입니다. python에서 등록한 키워드의 예는 다음과 같습니다.
continue , import , while , def , in , with ,del , is , yield , False , elif , lambda ,None , else , nonlocal , True , except , not ,and , finally , or , as , for , pass , assert ,from , raise , break , global , return ,class , if , try. 문자열이 키워드인지 어떻게 확인합니까?
파이썬에는 키워드와 관련된 특정 작업을 처리하는 내장 모듈
keyword이 있습니다. 문자열이 키워드인지 여부를 확인하는 데 사용되는 함수iskeyword()도 있습니다. 문자열이 키워드이면 true를 반환하고, 그렇지 않으면 false를 반환합니다.문자열이 키워드인지 아닌지를 알려주는 코드를 작성해 봅시다.
예시
# we need to import "keyword" for keyword operations
import keyword
# state the list of all string we want to test
str_test = ["in", "while", "loop", "break", "evening",
"elif", "assert", "maxwizard", "lambda", "else", "map"]
for i in range(len(str_test)):
# checking which are keywords
if keyword.iskeyword(str_test[i]):
print(str_test[i] + " is python keyword")
else: # if it is false then write
print(str_test[i] + " is not a python keyword")
산출:
in is python keyword
while is python keyword
loop is not a python keyword
break is python keyword
evening is not a python keyword
elif is python keyword
assert is python keyword
maxwizard is not a python keyword
lambda is python keyword
else is python keyword
map is not a python keyword
그러나 작업 중인 경우 이 모든 키워드를 변수로 사용하지 않도록 하려면 어떻게 해야 합니까? 변수 이름을 할당하는 동안 모든 것을 기억하는 것은 어려운 작업일 수 있습니다. 따라서 함수
kwlist()는 "keyword"모듈에 제공되어 33개의 모든 python 키워드를 인쇄합니다. 여기에서 인쇄하는 방법은 아래에 나와 있습니다.예2
#Python code on how iskeyword() works
# importing "keyword" for keyword operations
import keyword
# printing all keywords at once using "kwlist()"
print ("The list of keywords is below : ")
print (keyword.kwlist)
산출:
The list of keywords is :
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally',
'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
'nonlocal', 'not', 'or', 'pass', 'raise', 'return',
'try', 'while', 'with', 'yield']
kwlist는 파이썬에서 33개 키워드의 목록을 모두 반환합니다.
Reference
이 문제에 관하여(파이썬 키워드 인수), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/maxwizardth/python-keyword-arguments-3221텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)