26.10. Built-in Functions

Python has many built-in functions that are accessible from any namespace. Two of the most common are explained here:

The __init__ function is one of the most widely used functions in Python. Its purpose is to define what should be done when the class is instantiated.

__init__(self)

The built-in function dir() returns a list of strings representing the functions and attributes that the module defines. It is called like this:

>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__',
'__stdin__', '__stdout__', '_getframe', 'api_version', 'argv',
'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright',
'displayhook', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix',
'executable', 'exit', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags',
'getfilesystemencoding', 'getrecursionlimit', 'getrefcount', 'hexversion',
'maxint', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks',
'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval',
'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin',
'stdout', 'version', 'version_info', 'warnoptions']

After importing the sys module, any of these items may be called by the item name prefaced with sys and a period.

A more comprehensive list of the more popular built-in Python functions can be found in Appendix E, "Python Language Reference."

Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.