Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add builtin functions?

I am new to python programming. How can I add new built-in functions and keywords to python interpreter using C or C++?

like image 344
Rubia Avatar asked Aug 06 '11 05:08

Rubia


3 Answers

In short, it is technically possible to add things to Python's builtins, but it is almost never necessary (and generally considered a very bad idea).

In longer, it's obviously possible to modify Python's source and add new builtins, keywords, etc… But the process for doing that is a bit out of the scope of the question as it stands.

If you'd like more detail on how to modify the Python source, how to write C functions which can be called from Python, or something else, please edit the question to make it more specific.

If you are new to Python programming and you feel like you should be modifying the core language in your day-to-day work, that's probably an indicator you should simply be learning more about it. Python is used, unmodified, for a huge number of different problem domains (for example, numpy is an extension which facilitates scientific computing and Blender uses it for 3D animation), so it's likely that the language can handle your problem domain too.

†: you can modify the __builtin__ module to “add new builtins”… But this is almost certainly a bad idea: any code which depends on it will be very difficult (and confusing) to use anywhere outside the context of its original application. Consider, for example, if you add a greater_than_zero “builtin”, then use it somewhere else:

$ cat foo.py
import __builtin__
__builtin__.greater_than_zero = lambda x: x > 0

def foo(x):
    if greater_than_zero(x):
        return "greater"
    return "smaller"

Anyone who tries to read that code will be confused because they won't know where greater_than_zero is defined, and anyone who tries to use that code from an application which hasn't snuck greater_than_zero into __builtin__ won't be able to use it.

A better method is to use Python's existing import statement: http://docs.python.org/tutorial/modules.html

like image 53
David Wolever Avatar answered Oct 19 '22 02:10

David Wolever


for python 3.6 onward use import builtins.


# example 1

import builtins

def f():
    print('f is called')

builtins.g = f

g() # output = f is called

####################################
# example 2

import builtins

k = print

def f(s):
    k('new print called : ' + s)

builtins.print = f

print('abc') # output = new print is called abc


like image 45
praveen chaudhary Avatar answered Oct 19 '22 01:10

praveen chaudhary


While David Wolever's answer is perfect, it should be noted again that the asker is new to Python. Basically all he wants is a global function, which can be done in two different ways...

  1. Define a function in your module and use it.
  2. Define a function in a different module and import it using the "from module import *" statement.

I think the asker's solution is the 2nd option and anyone new to Python having this question should look in to the same.

For an advance user, I would agree with Wolever's suggestion that it is a bad idea to insert a new function in to the builtin module. However, may be the user is looking for a way to avoid importing an always-used module in every script in the project. And that is a valid use case. Of course the code will not make sense to people who aren't part of the project but that shouldn't be a concern. Anyways, such users should look in to the PYTHONSTARTUP environment variable. I would suggest looking it up in the Index of the Python documentation and look at all links that talks about this environment variable and see which page serves your purpose. However, this solution works for interactive mode only and does not work for sub-main script.

For an all around solution look in to this function that I have implemented: https://drive.google.com/file/d/19lpWd_h9ipiZgycjpZW01E34hbIWEbpa/view

Yet another way is extending or embedding Python and it is a relatively complex topic. It is best to read the Python documentation on the same. For basic users, all I would say is that...

  • Extending means adding new builtin modules to the Python interpreter.
  • Embedding means inserting Python interpreter into your application.

And advanced users already know what they are doing!

like image 31
RcnRcf Avatar answered Oct 19 '22 01:10

RcnRcf