"object" vs "dynamic" programming styles

Just van Rossum just at letterror.com
Sat Mar 24 17:45:39 EST 2001


"Edward C. Jones" wrote:
 
> Here are three methods for doing the same thing. When is it better to use
> one of the approaches rather than another?

> # Program 3
> 
> # The function New_X returns an instance of either class A or class B.
> def New_X(c):
>     if c == 'a':
>         return A()
>     if c == 'b':
>         return B()
 
My personal preference would definitely be version 3, the factory function.
Less magic, clearer code.

When I first started writing OO code (which was with Python) I was often
tempted to do lots of smart things in the __init__ method. But it only
makes things harder in the long run. I recently had to extend a module I
had written a few years back: all the smart things in the __init__ were
of no use to me now: it assumed a quite different usage.
So I changed the __init__ to be as simple as possible, and added a couple
of factory functions. It's all nice and clean now...

Just



More information about the Python-list mailing list