Why a class when there will only be one instance?

Christian Tismer tismer at stackless.com
Thu May 27 19:06:47 EDT 2004


Vineet Jain wrote:

>>Just a little observation:
>>If you really want the class and the instance to be the same,
>>well, you can take a module.
>>In a sense, this *is* a class and the only instance.
> 
> 
> I need to use BASIC language like scripting in a python application. The 
> users are not programmers and the scripting interface needs to be 
> as simple as possible. The script would have a init section
> a run section a close section and some other parts as well. I 
> have these sections defined as functions in a class (the class inherits
> from a base class) currently but have a feeling that I'm going to have 
> to simplify things. Am not sure how users will handle class, functions, 
> self, import, etc.

I understand!

> I was thinking of using a module, but can I repeatedly call the module. 
> If so how would I call it? Would I keep reloading it? Where does a module
> store it's state information between calls?

Ok, in that sense I was wrong: by definition, a module is not callable,
and it is run exactly once, usually just to make its definitions
which you then call from outside.

The module isn't much more than an object wrapped around a dict.
All globals are in that dict, including the defined function
object, and that's it.
It is easy to take a module like you described, and "run" it
again, given that you know what's in it.
Just keep track of the state you want to preserve and
you want to re-initialise, and then call the different parts
from outside. Easy.
But, you probably want to even go without functions?
Then you would have to re-execute all code of the module
each time, and well, you can use reload for this,
but I would probably write my own evaluator for this,
not importing the module directly, but compiling its code
once, and then execute the code, give them special globals
with custom builtins, to protect you from non-programmers.

  I was thinking of supporting
> different sections by setting different variables which the user would 
> access it:
> 
> if init:
> 	do init code here
> 
> if run:
> 	do run here
> 
> if close:
> 	do close here
> 
> Any other ideas? I'm trying to keep the scripting interface as simple as
> possible but at the same time give the users the power of python.

Why nopt, and yes, a module is just fine, better to say, it is not a module,
but a piece of python source with a defined structure, and you
compile it and run it explicitly.

ciao - chris
-- 
Christian Tismer             :^)   <mailto:tismer at stackless.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34  home +49 30 802 86 56  mobile +49 173 24 18 776
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
      whom do you want to sponsor today?   http://www.stackless.com/





More information about the Python-list mailing list