Saturday, August 26, 2017

Python: tips for variables

키워드를 변수로 사용 : SyntaxError
키워드 확인
>>> import keyword
>>> keyword.kwlist

module 이름을 변수로 사용: 모듈사용못함, 하면 TypeError
>>> abs=1 # now abs type is 'int'
# How to use abs() function?

>>> abs = 1
>>> type(abs)
<type 'int'>         # abs is int variable
>>> abs(-2)        # TypeError

Traceback (most recent call last):
  File "<pyshell#137>", line 1, in <module>
    abs(-2)
TypeError: 'int' object is not callable
>>> del abs       # delete int variable; remain internal function abs()
>>> type(abs)
<type 'builtin_function_or_method'>
>>> abs(-2)
2
>>>

No comments:

Post a Comment