파이썬(python) – 내장함수(builtin function) 리스트 보기

len 이나 type 등 제가 강의를 진행하면서 아무런 모듈도 import 하지 않았는데 수행이 즉시 가능한 함수들이 있었죠 ?
이런 함수들을 builtin 함수라고 합니다.
len 이나 type 뿐 아니라 dir, help 모두 마찬가지겠죠? 아무런 모듈없이 그냥 사용했잖아요 ? ^^

그 리스트는 dir(__builtin__) 명령을 통해 확인 가능합니다. builtin 함수의 사용법 및 설명을 보고 싶으면 제 강의에서 수차례 언급한대로 help() 를 사용하시면 됩니다.

>>> dir(__builtins__)

['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']

위에 제가 언급했던 함수들이 모두 들어있죠 ? ^^
아... 우리가 변수값을 보고자 할 때 사용했던 print 도 있네요.

이 밖에도 string 을 integer 타입으로 변환해주는 int 라던가, file 을 열어서 file object 를 리턴해주는 open 이나, 현재 scope 에서 선언된 global 변수와 값을 dictionary 형태로 보여주는 globals() 함수 등 아주 유용한 것이 많습니다.

python interpreter 환경에서 프로그램을 마구 짜다가 문득 지금까지 내가 선언하거나 값을 할당한 변수가 무엇무엇이 있는지 확인하고 싶을 때 globals() 하고 입력하면 이를 모두 dictionary 로 볼 수 있습니다.

우리가 print 를 입력하지 않고 그냥 변수 이름만 치면 그 값이 바로 튀어나오는 것도 바로 이 globals dictionary 가 있기 때문입니다. key 를 입력하면 바로 그 값이 나오는 것이죠.

위의 함수들 사용법을 help 로 확인하시고 이것저것 시험해 보세요. ^^

You may also like...

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x