Wrap Python built-in functions

If you create a function with the same name as a built-in function in Python and want to call the original built-in function in that function, the builtins module (manual You can call built-in functions by using library / builtins.html)).

For example, if you want to wrap the built-in function max and use it, you can write as follows.

sample.py


# -*- coding: utf-8-unix -*-
import builtins

class Foo(object):
    pass

def max(a, b):
    u"""A function that wraps a built-in function
    """
    v1, v2 = a, b

    if isinstance(a, Foo):
        v1 = a.value

    if isinstance(b, Foo):
        v2 = b.value

    #Calling built-in functions
    return builtins.max(v1, v2)

if __name__ == '__main__':
    obj = Foo()
    obj.value = 10

    print(max(20, obj))  # >>> 20
    print(max(5, obj))  # >>> 10
    print(max(5, 20))  # >>> 20

In Python 2 series, the same thing can be achieved by using the __builtin__ module (**'s' is not added! **).

If you want to be able to execute in both Python 2 series and 3 series, you can load the module as follows.

try:
    # Python 3.For X
    import builtins
except ImportError:
    # Python 2.For X
    import __builtin__ as builtins

Recommended Posts

Wrap Python built-in functions
Python built-in functions ~ Zip ~
Built-in python
Python functions
Built-in functions
Python built-in object
Python built-in object
#Python basics (functions)
[Beginner] Python functions
Python Easy-to-use functions
Python basics: functions
Python's built-in functions
Conquer 69 Python built-in functions 6th p ~ r
Correspondence between Python built-in functions and Rust
Python Beginner's Guide (Functions)
Python basic course (12 functions)
[Python] Memo about functions
# 4 [python] Basics of functions
Useful Python built-in functions that you use occasionally
Curry arbitrary functions with Python ....
Python> lambda> tiny functions / callback functions
Getting Started with Python Functions
Python3 programming functions personal summary
Various Python built-in string operations
Study from Python Hour3: Functions
Overriding library functions in Python
Wrap long expressions in python
Keyword arguments for Python functions
Python for super beginners Python #functions 1
Python 3 sorted and comparison functions
Python functions learned in chemoinformatics
Python higher-order functions and comprehensions
[python] Manage functions in a list
Python
About python dict and sorted functions
Examine built-in functions (standard built-in functions) from globals
Using global variables in python functions
10 functions of "language with battery" python
Dynamically define functions (methods) in Python
Python3> Functions> Function Renaming Mechanism> f = fib
[Python3] Dynamically define global variables in functions
Build an environment for Blender built-in Python
[Introduction to Udemy Python3 + Application] 55. In-function functions
Easily use your own functions in Python
Pharmaceutical company researchers summarized functions in Python
[Control engineering] Graphing transfer functions using Python
Try using Python with Google Cloud Functions
New features in Python 3.4.0 (3)-Single-dispatch generic functions
Summary of built-in methods in Python list
[Python] Understand how to use recursive functions
Use Python and MeCab with Azure Functions