[Tutor] Modifying Source Code while Program is Running

Alan Gauld alan.gauld at freenet.co.uk
Sat Nov 26 13:27:15 CET 2005

>> point. Classes express behaviour, the data is only there to support
>> the behaviour. Thats why methods are polymorphic but not attributes.
>If classes express behaviour, then what expresses the structure of the
> data?  

Why do you care? If the program behaves as you expect it what does 
it matter what data it uses or how. That should be hidden from you 
inside the classes. Worrying about data is a feature of traditional 
Information Modelling/Structured Analysis style programming, 
OOP is all about inter-communicating objects sending messages to 
each other, each requesting and providing services to the others.

> (ie what attributes there are going to be and what values they
> are likely to accept).

You need to think about the objects that you pass around as part 
of the messages, but those obnjects are in turn accessed via messages 
so their structure is not important. What does matter is the 
relationships between the objects, and they are related by message 
paths not fixed data relationships. This switch in thinking is the 
fundamental difference between traditional and OOP design.

> You need (the option of) a data definition in order to generalise. 

No, you need the ability to modify behaviour.
Most traditional programmers think in terms of modifying 
behaviour in terms of big if/elif trees

if obj.type == sometype
elif obj.type == another

Whereas in OOP you simply say


And the right kind of doThing will happen because obj 
knows how to respond to that message in the appropriate way.

> Using my web server example from earlier, you need to be able to say
> that for any type of page, whatever it's attributes, you can create a
> web form to search for that type of page by iterating through it's
> attributes and creating a relevant form field for each type.

So you want, for a given page to create a Form and search.

But that's behaviour of the Page, just let it create its own form
and do its own searches, not your problem. Create the right 
kind of Page and it will do the work for you...

> I definitely had the opposite in mind.  The behaviours of all the
> classes would be the same (and would be fairly simple, a method for
> rendering the page with a template, a method to update the data based
> on a form submission).  

Thats fair enough and the parent Page class would do just that, 
but the template class would have methods that the Page called, 
and you can subclass or data drive the templates to return the 
appropriate strings to the page. (Since web pages after all are 
just collections of strings - or evenone big string...)

> Users would basically be able to change the
> data structure (add and remove attributes).

So maybe attributes are objects too? Maybe you page needs 
to know how to handle attributes and you can create pages by 
adding attributes from a pick list, each attribute knowing how 
to render itself and how to respond to searches?

There are lots of ways to address this is we think of the world 
in terms of intercommunicating objects not static data structures 
with a few overeaching functions controlling it all.

> I'm sure Python is quite possibly the least worst at this, but that
> doesn't make it good at it.

Which brings me back to my original question, what environment 
do you think is good at it? Are you aware of such an environment 
or merely wishing that such a thing could be invented?

> Again, I'm sure it is better than C, but I definitely have had the
> feeling that this is not something the language was designed for, 
> and that not many other people seem to be trying it.

The language is a general purpose language like C and as such is 
designed to cover that type of operation, however the reason it's 
not generally used for long running server processes is the same 
reason that most of these are written in C: speed and resource usage.

Server processes are usually required to run in the background, 
getting in the way as little as possible (think print spoolers), or to 
serve large numbers of clients requesting services(think databases)
In the first case they need to consume as few resources as possible 
and in the latter they need to be low resource and very high speed.

Because python is interpreted it tends to be a lot slower than C 
and because it needs the interpreter to be loaded in memory its 
also quite hih resource usage, so in that sense its not ideal for 
server or daemoin usage, but the language itself is as good as 
any other if speed/resources are not the key issues.

Alan G

More information about the Tutor mailing list