Extending Python by Adding Keywords & Data types

Paul McGuire ptmcg at austin.rr.com
Wed Aug 1 15:08:07 CEST 2007

On Jul 31, 3:28 pm, Maximus Decimus <vishak.1... at gmail.com> wrote:
> I am using python v2.5 and I am an amateur working on python. I am
> extending python for my research work and would like some help and
> guidance w.r.t this matter from you experienced python developers.
> II want to add some more KEYWORDS and DATATYPES into the python script
> apart from the existing ones.

Vishak -

Let me echo the sentiments of the other posters who replied to your
message.  It is *very* unusual for a new Python user to begin their
Pythonic journey by modifying the Python interpreter to add new
keywords and datatypes.  Python is designed to be a very flexible and
extensible language just as it is.

Since you are familiar with C, your question strikes those of us on
this group as one who would write "I'm just learning C and I would
like to modify the core library to add some new functions for my
research."  And all the readers on comp.lang.c scratch their heads,
thinking "Huh?  Why doesn't this guy just write the functions in his
own program, the way the language was designed for him to do in the
first place?"

Now maybe your research is in the field of language design, and has
something to do with the relative ease/difficulty of modifying
computer languages.  Then it would make sense for you to dive right in
to learning how to modify Python's compiler.

But if your research was in just about any other field (from finite
elements analysis to genetic programming to genetics to simulations to
process control to whatever), DON'T start by modifying Python - start
by LEARNING Python.

When a new datatype is required in a Python program, then the
programmer writes a class to implement this datatype.  The class
itself is written in Python, but it can be used just like any built-in
datatype.  For instance, here is a new datatype I just thought up - a
Box that can hold up to 'n' objects.

class Box(object):
    def __init__(self,n):
        self.capacity = n
        self.contents = []

    def add(self,other):
        if len(self.contents) < self.capacity:
            self.contents.append( other )
            raise ValueError("can't add any more to this Box")

    def __iadd__(self,other):
        return self

box = Box(3)

# add stuff to the box until it overflows
    box += object()

Voila!  I created a new datatype, Box, and even added support for it
to understand how to use the '+=' operator so that adding objects to
the Box looks like an addition statement.  All without modifying
Python itself.  (Here's an exercise - extend this example so that the
Box has a limited capacity in the *weight* of the added objects, as
well as in the number of objects.)

This is the basic way that one extends Python with their own new
datatypes and methods.  New keywords are a little rarer, but really,
start by just adding methods, like the add method above.  Python users
around the world develop a wide range of applications and programs
using just these techniques, and never touch the Python compiler

And here is a case you'd like to avoid.  Let's say you start by
learning how to modify Python because you need a general-purpose
container for things.  You spend two weeks learning how to do this,
getting your code mostly debugged, and then you post to
comp.lang.python your proud achievement.  Immediately the replies come
back, "Congratulations, newbie, you just reinvented the built-in list
type."  Without LEARNING Python, you wont know what is already
provided in the language.

So, in general this is a helpful group, and it is possible that you DO
need to learn how to add datatypes and keywords to Python as your
first objective.  We're not trying to pry, but give us a bit more
detail.  Someone might even rough out for you what one of your new
datatypes might look like.  So pray, tell us what sort of specialized
datatypes and keywords do you think you need to add, and we'll try to
point you in one or more directions.

-- Paul

More information about the Python-list mailing list