Re: Using code objects?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Using code objects?
On 6/21/05, Chinook [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Using code objects?
On Tue, 21 Jun 2005 09:56:27 -0400, Konstantin Veretennicov wrote (in message [EMAIL PROTECTED]): On 6/21/05, Chinook [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Using code objects?
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 -- http://mail.python.org/mailman/listinfo/python-list