[Tutor] Modifying Source Code while Program is Running

Alan Gauld alan.gauld at freenet.co.uk
Fri Nov 25 19:43:43 CET 2005


Hi Ed,

This is a longish response because you are raising some 
very interesting (and deep) issues from a computer science 
point of view.

> Well, self-modifying isn't inherently necessary.  What I guess I
> really need is persistent classes as well as persistent objects.

Python classes can be persistent just like any other object, 
theres nothing intrinsically different between a class object 
and any other kind of object (except that classes as objects 
are kind of mind bending as a concept!)

> I always tend to think of classes as templates for objects rather 
> than factories.  

They are both.

> In my mind, object methods are just calls to the class which 
> are evaluated every time they are called.  

The methjods are function attributes of a class which are selected 
by the class dispatch mechanism in response to messages sent to 
instances. In Python that means the getattr() methods look up 
the right method in response to receipt of a message.

> Objects should be strict instances of classes that can't be 
> modified except for the values of their attributes.

Attributes in a pure OOP language include both the contained data 
and functions. In other workds methods can be added/removed and 
modified as well as the data. In fact many would argue that the 
internal data not only cannot be modified externally but should not 
even be seen - the Data Hiding school of thought.

> Class Page(object):
>    def print(self):
>         printPage(self)

What does that gain you?

> And have all my methods call functions (passing on parameters as
> necessary).  That way if I change a function, it will be changed for
> every instance of every object of that class.

You mean like this:

>>> class C:
...   def m(self): print 'm'
...
>>> def f(o): print 'other m'
...
>>> c = C()
>>> c.m()
m
>>> C.m = f
>>> c.m()
other m
>>>

> And couldn't I write a function that would add functions or attributes
> to classes and objects?

Of course, just like I did there.

> Am I trying to use the wrong language for this?  I love Python but I
> seem to keep coming up against lots of practical issues with it 

Python is a pretty good language for this kind of thing, 
its just that this kind of thing is intrinsically difficult. 
The only lanuages I know of which are better than Python 
at it are Lisp and Smalltalk (and even Smalltalk 
has some limitations compared to Python). Do you have 
experience of a language which is more flexible in this regard? 
I'm curious because you seem to assume that its almost 
normal to be able to do these things, whereas in my 
experience its usually impossible rather than just difficult...

> define the behaviours I want without having to bother with how the
> computer is actually going to handle them.

Believe it or not thats been the holy grail of language 
designers since the days of COBOL - The Common Business 
Oriented Language - which allowed business people to 
write theitr own programmes in "standard, everyday English", 
like:

PROGRAM ID - HELLO

PROCEDURE DIVISION
   MAIN-LINE-MODULE
       MOVE 0 TO COUNTER
       OPEN INPUT IN-FILE
       PERFORM READ IN-FILE UNTIL END-OF-FILE = "YES"
       ADD 1 TO COUNTER
       DISPLAY "LINE NUMBER ", COUNTER
       CLOSE IN-FILE
       STOP RUN.

At the time it was considered revolutionary and programmers 
trembled at the prospect of instant redundancy. As it happened 
your average businessman found COBOL about as readable as pigin 
English! But that hasn't stopped us trying...

One thing we have discovered is that the more natural the 
language seems to get the less flexible the language gets. 
Python is the closest I've seen yet to a general purpose 
language that is truly readable, but thats still a long 
way from being plain English! But I digress :-)

Basically I just wonder what your background is that you 
seem to expect these kind of dynamic facilities? I'd certainly
be interested in investigating it further.

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051125/d841fa0b/attachment.html


More information about the Tutor mailing list