any macro-like construct/technique/trick?

alex23 wuwei23 at gmail.com
Wed Jun 1 23:23:41 EDT 2005


Mac wrote:
> * using
>    def debug_emit(obj):
>        if debug:
>            emit_dbg_obj(obj)
> is a poor solution, because it *always* instantiates DbgObj*, even when
> not needed; I want to avoid such unnecessary waste

Rather than passing in an instantiated object and only operating on it
if the debug flag is set, how about passing in the class &
initialisation parameters and only creating the object if necessary?

>>> def debug_emit(cls, args):
... 	if debug: emit_dbg_obj(cls(*args))

Then your calls will just be a single line:

>>> debug_emit(DbgObjFoo,(a,b,c))

Does that help at all?

-alex23




More information about the Python-list mailing list