Class optimization at runtime

Larry Bates lbates at swamisoft.com
Mon Aug 2 23:06:03 CEST 2004


Kind of hard to tell what you are trying to accomplish
but I'll try.  Define the class with no if/else statements.
They only get parsed once.  It is the "class instance"
creations that may get created millions of times.  They
will be inside your logical structure.

class Foo:
    def __init__(self):
        # do stuff

class Bar:
    def __init__(self):
        # do stuff

if option: x=Foo()
else:      x=Bar()

HTH,
Larry Bates


<brianc at temple.edu> wrote in message
news:mailman.1061.1091479321.5135.python-list at python.org...
> I would like to optimize my classes at run time. The question
> is where to put my "if" statement.
> -Inside __main__?
> if option:
>     class Foo:
>         def __init__:
>             #do stuff
> else:
>     class Foo:
>         def __init__:
>             #do other stuff
> -Inside class definition?
> class Foo:
>     if option:
>         def __init__:
>             #do stuff
>     else:
>         def __init__:
>             #do other stuff
> -Inside each method definition?
> class Foo:
>     def __init__:
>         if option:
>             #do stuff
>         else:
>             #do other stuff
>
> Class instances will be created millions of times and the
> method's on them called even more so whatever brings the most
> speed will be used. I fear that the more readable way to do it
> is also the slowest.
>
> Thanks in advance,
> -Brian





More information about the Python-list mailing list