Search code examples
pythonpython-3.xlistbuilt-in

how to modify python's built in modules


I want to add functions to python's built in module. As a example, there is no find function in list. I want to add it. There is some other functions I want to add. Can somebody please tell me how can I do that? Is their any way I can do that?


Solution

  • Adding functions to the builtins module is simple:

    import builtins
    builtins.find = my_find_function
    

    But the built in classes are usually immutable, so they cannot be changed.

    >>> list.hello = "hello"
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can't set attributes of built-in/extension type 'list'