Solution 1:

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

Solution 2:

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