Where are the built in functions in python?

where the built in function was written?>
like in which class?
how can i write len()
and not object.len()?

The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.

https://docs.python.org/3/library/functions.html

To wit:

len ( s )

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

We see multiple classes upon which this function may be called. It is not a method on any of them, hence we cannot write,

object.len()

how python wrote this build in function?
are they in the main class?

Being at the thin end of the stick, I couldn’t possibly give an answer to that question. There is a lot going on under the hood in Python, or any programming language for that matter. We beginners have our hands full just learning the basics like syntax, data types, objects, functions, and the like.

Not sure what you’re asking, if it’s about that specific function or all built-in functions or functions in general and whether you’re asking about where the source code is or where things can be found while running the program or … maybe you’re asking about some specific detail about what makes it work …

Same as with any other function class or what have you, it’s defined in a module, for builtins that module is … builtins.

Which means we can import it…

>>> import builtins
>>> 

And therefore means we can get a list of contents…

>>> dir(builtins)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
>>> 

for example in java to print we write- system.out.print("")
so i know im looking for method inside class system(i dont know java just for example)

but in python i write
print()

where is this functhion come from? is it inside a class?

python isn’t java, there’s no main class, things don’t need to be in classes
and if it was from a class then that only moves the question to where is that class? is that class in a class? and so on

what are you really wondering?

the source for print is here:
https://github.com/python/cpython/blob/c6dedde160a9fce5d049e860f586ad8f93aec822/Python/bltinmodule.c#L1827

1 Like