Extending Python by Adding Keywords & Data types

Maximus Decimus pingmaximus at gmail.com
Wed Aug 1 16:10:17 EDT 2007


On Aug 1, 9:08 am, Paul McGuire <pt... at austin.rr.com> wrote:
> 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 )
>         else:
>             raise ValueError("can't add any more to this Box")
>
>     def __iadd__(self,other):
>         self.add(other)
>         return self
>
> box = Box(3)
>
> # add stuff to the box until it overflows
> while(True):
>     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
> itself.
>
> 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

On Aug 1, 9:08 am, Paul McGuire <pt... at austin.rr.com> wrote:
> 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 )
>         else:
>             raise ValueError("can't add any more to this Box")
>
>     def __iadd__(self,other):
>         self.add(other)
>         return self
>
> box = Box(3)
>
> # add stuff to the box until it overflows
> while(True):
>     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
> itself.
>
> 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


Firstly to Paul, I clearly got your reply and I am working hard to
know more about python by going through the tutorials. I did start
with the doc tutorial available along with the package. I have done
the basic learning of python which is way simple compared to the
stuffs you are talking about. I have just jumped into the extending
python part of the document. I think I need to master that portion.
Your code snippet was quite simple and it explained me very well than
the tutorial. HAts off to u!!

Bjorn, Marc, Ben, I also noted your comments and I am working on them.
I have gone through the Python Classes portion and I need to workout
quite a lot excercises to make myself familiar with Python. I am gonna
start doing that right away.

Coming to the complete explanation of what I am upto:

I am doing my research in Pervasive space environments filled with
sensors and actuators. I am just trying to modify an existing language
which suits that kind of environment. Pervasive space environment is
filled with sensors and actuators and everything depends on their
state and behavior ie on the sensor readings and actuator states. So
why not express a language variable in terms of sensors and actuators?
THis is my idea of working around and trying to introduce new data
types for these sensor and actuator variables which store sensor
readings and actuator states.

I think this explains a bit clearly on what I am looking
for..Suggestion and ideas are kindly appreciated.

Thanks in advance and for your time.




More information about the Python-list mailing list