[Tutor] Modifying Source Code while Program is Running

Ed Singleton singletoned at gmail.com
Sat Nov 26 21:44:34 CET 2005


On 25/11/05, Alan Gauld <alan.gauld at freenet.co.uk> wrote:
>
> 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...

My background is in languages that are horrifically worse than python,
but I'm overly demanding I guess.  I think I have a good instinct for
what computers should be doing for me.  Everytime I'm doing something
repetitious I get exasperated because I know the computer should be
performing the repetitions rather than me.

I had the misfortune a couple of days ago of having to knock up a
couple of web pages in ASP.  A very basic search form, results page
and details page.  I had to open up a connection to the database, open
a recordset, concatenate a strings to make an SQL query, etc, etc.

It was horrible.  I copied and pasted a lot of the code, but even so
it involved doing ridiculous amounts of unnecessary work.

Maybe I'm just lazy, but then most progress has been made by people
who were either curious or lazy.

> > 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 :-)

I can quite imagine that natural language isn't the way forward,
though I can also imagine that for business reasons readability won't
be the way forward (however I do love readability).

What's important is not natural language but eliminating unnecessary
work and complexity.

> 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.

Maybe it's just knowing what'll be possible in 10 years time and being
impatient for it.

Ed


More information about the Tutor mailing list