+ Development/Python

[Python] 변수명(함수명, 클래스명등) 만드는 법

:: Teacher :: 2018. 9. 23. 18:15
728x90
반응형

변수명을 사용하기 위해서는 예약어 및 내장 함수, 모듈명으로는 사용하지 않는 게 좋다. 

또한, 변수명으로 내장 함수명(혹은 모듈명)을 사용했을 경우 아래와 같이 해당 함수(혹은 모듈)의 역할을 정상적으로 할 수 없다.

1. 예약어 확인 방법

>>> import keyword

>>> keyword.kwlist

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

2. 내장 함수명(혹은 모듈명)을 사용 예제

>>> str(1234)

'1234'

>>> print type(1234)

<type 'int'>

>>> print type(str(1234))

<type 'str'>

>>> str = 'aaa'

>>> str(1234)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'str' object is not callable

 

728x90
반응형