FAQ
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

Search Discussions

  • Konstantin Veretennicov at Jun 21, 2005 at 1:56 pm

    On 6/21/05, Chinook wrote:
    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:
    ...
    obj1 = compile(exp1, 'whatever', 'single')
    ...
    obj2 = compile(exp2, 'whatever', 'exec')
    Are you essentially asking about difference between compile(..., 'single')
    and compile(..., 'exec'), which is described in
    http://docs.python.org/lib/built-in-funcs.html ?

    - kv
  • Chinook at Jun 21, 2005 at 9:41 pm
    On Tue, 21 Jun 2005 09:56:27 -0400, Konstantin Veretennicov wrote
    (in message <4660fe3005062106565aab8536 at mail.gmail.com>):
    On 6/21/05, Chinook wrote:

    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:
    ...
    obj1 = compile(exp1, 'whatever', 'single')
    ...
    obj2 = compile(exp2, 'whatever', 'exec')
    Are you essentially asking about difference between compile(..., 'single')
    and compile(..., 'exec'), which is described in
    http://docs.python.org/lib/built-in-funcs.html ?

    - kv
    [I neglected to post this to the list]

    Sorry Konstantin, being way too late at the time I missed the obvious. Since
    I'll have multiple statements in a code object I will, of course, use 'exec'

    In the meantime I have done a lot of searching and I guess the second point
    of the query is pretty well settled also, unless you have another thought.
    There are some number of code objects independent of some number of potential
    classes. Each potential class "action' method will return one or more of the
    code objects. So I 'compile' the code objects separately and pass back
    appropriate references from the factory derived class method.

    I'm intentionally over designing a simple utility so I will learn the
    techniques and alternatives for a more involved application that involves AI.
    Thanks,
    Lee C
  • Fredrik Lundh at Jun 22, 2005 at 8:06 am

    Chinook wrote:

    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)?
    from where are you getting the source code for those code objects?

    from the example below, it sure looks like using callable objects and
    argument binding is a "better way" to do it. for the simplest cases,
    you can use a plain lambda to delay evaluation:
    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
    ...
    obj1 = lambda: foo("#expression1#")
    obj1()
    #expression1#
    obj2 = lambda: foo("#expression2#")
    obj2()
    #expression2#

    </F>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 21, '05 at 3:45a
activeJun 22, '05 at 8:06a
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase