I would like to use getattr()
to access Python's built-in functions. Is that possible?
For example:
getattr(???, 'abs')
I know I can just simply do:
>>> abs
<built-in function abs>
But I want to use getattr
, because the keyword names are strings.
builtins
module:You could try importing builtins
module:
>>> import builtins
>>> getattr(builtins, 'abs')
<built-in function abs>
>>>
As mentioned in the documentation:
This module provides direct access to all ‘built-in’ identifiers of Python; for example,
builtins.open
is the full name for the built-in functionopen()
. See Built-in Functions and Built-in Constants for documentation.
So the above mentions that builtins.open
is the open
function. So abs
is the same builtins.abs
is the same thing as abs
. But for gettatr
, getattr(builtins, 'abs')
is also the same as builtins.abs
.
__builtins__
(NOT RECOMMENDED):You could try getting from the __builtins__
:
>>> getattr(__builtins__, 'abs')
<built-in function abs>
>>>
As mentioned in the documentation:
CPython implementation detail: Users should not touch
__builtins__
; it is strictly an implementation detail. Users wanting to override values in thebuiltins
namespace shouldimport
thebuiltins
module and modify its attributes appropriately.The
builtins
namespace associated with the execution of a code block is actually found by looking up the name__builtins__
in its global namespace; this should be a dictionary or a module (in the latter case the module’s dictionary is used). By default, when in the main module,__builtins__
is the built-in modulebuiltins
; when in any other module,__builtins__
is an alias for the dictionary of thebuiltins
module itself.
As you can see, it's not recommended, also usually the __builtins__
is a dict
, rather than a module.
If you write your code in modules, __builtins__
would return dictionary aliases of the builtins
module, which would give something like: {..., 'abs': <built-in function abs>, ...}
.
getattr
:Just to have more idea about getattr
, as mentioned in the documentation:
Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example,
getattr(x, 'foobar')
is equivalent tox.foobar
. If the named attribute does not exist, default is returned if provided, otherwiseAttributeError
is raised.
So:
>>> import builtins
>>> getattr(builtins, 'abs')
<built-in function abs>
>>>
Is the same as:
>>> import builtins
>>> builtins.abs
<built-in function abs>
>>>
So you might be wondering since:
>>> abs
<built-in function abs>
Gives the same thing, why we can't just do:
getattr(abs)
The reason we can't do that is that that getattr
is suppose to be calling methods/functions/classes of a classes/modules.
The reason using getattr(builtins, 'abs')
works is because builtins
is a module and abs
is a class/method, it stores all the built-in Python keywords as methods in that module.
All the keywords are showed on this page of the documentation.