본문 바로가기
프로그래밍/Python

변수명

by 체리 2011. 3. 5.
반응형

일반적인 언어의 변수 명명 방법과 차이를 보이지는 않는다.
다른 언어도 마찬가지로 예약어는 사용할 수가 없는데 다음과 같이 예약어를 확인할 수 있다.
>>> import keyword
>>> keyword.kwlist
['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']
>>> len(keyword.kwlist)
33

예약어에 assign 하려 한다면 다음과 같은 오류를 볼 수 있다.
>>> False = '1'
SyntaxError: assignment to keyword
>>> False = a
SyntaxError: assignment to keyword
>>> False = 1
SyntaxError: assignment to keyword

단, 조심할 것이 있는데 내장 함수 이름을 모르고 변수명으로 이용하게 되면 해당 내장 함수를 사용할 수 없게 된다.
>>> str(123)
'123'
>>> str = 'test'
>>> str(123)
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    str(123)
TypeError: 'str' object is not callable

반응형

'프로그래밍 > Python' 카테고리의 다른 글

github의 pulls, issues 가져오기  (0) 2022.05.12
콘솔 입출력  (0) 2011.03.06
기초문  (0) 2011.03.05
Python 기본  (0) 2011.03.05

댓글