[Tutor] Using code objects?

Chinook chinook.nr at tds.net
Tue Jun 21 07:57:51 CEST 2005


Using code objects?
===================

As an OO exercise I have a factory pattern that returns class objects that 
each have an "action" method.  ClassObj.action() in turn returns a code 
object in my recursive process loop.

I create the code objects as a one time step outside my factory pattern and 
potential class definitions, then reference them in my potential classes 
which seems to work as expected.  

When I create the code objects though, it seems a couple different ways work 
and I'm wondering which is better and why (or is there a more correct 
technique in this situation)?

The two different ways are illustrated below:

Python 2.4.1 (#2, Mar 31 2005, 00:05:10) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)]
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo(st):
...   print st
... 
>>> exp1 = 'foo("#expersion 1#")'
>>> exp2 = 'foo("#expersion 2#")'
>>> obj1 = compile(exp1, 'whatever', 'single')
>>> exec obj1
#expersion 1#
>>> obj2 = compile(exp2, 'whatever', 'exec')
>>> exec obj2
#expersion 2#
>>> 

Thank you,
Lee C




More information about the Tutor mailing list