본문 바로가기
+ Development/Python

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

by :: Teacher :: 2018. 9. 23.
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']

728x90

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
반응형

'+ Development > Python' 카테고리의 다른 글

[Python] 연산자 정리  (0) 2018.09.23
[Python] 자료형 확인  (0) 2018.09.23
[Python] MySQL-Python 설치  (0) 2018.09.23
[Python] Virtualenv 설치 및 사용법  (0) 2018.09.22
[Python] 개행 되지 않고 변화랑 프린트하기  (11) 2018.09.09

댓글


loading