Class design question
Adam Lanier
adam at krusty.madoff.com
Wed Oct 3 15:38:34 EDT 2007
On Wed, 2007-10-03 at 18:47 +0000, George Sakkis wrote:
> >
> > I would use variable argument list for this; it's also consistent
with
> > your example Foo( 'baz', Bar( 'something else' )), otherwise you
need
> > to call it as Foo([ 'baz', Bar( 'something else' ) ])
Good point, this is what was tripping me up...
> >
> > # always inherit from object unless you have a good reason not to
> > class Foo(object):
> >
> > # XXX this is a class instance, shared by all Foo instances;
> > # XXX probably not what you intended
> > params = [ ]
> >
> > def __init__(self, *args):
> > # uncomment the following line for instance-specific params
> > # self.params = []
> > for arg in args:
> > if not isinstance(arg, Bar):
> > # let the Bar constructor to do typechecking or
whatnot
This is also tangentially what I was asking, Should type-checking be
done in the caller or the callee (so to speak). I guess good OOP
practice would be to push it down the call stack.
> > arg = Bar(arg)
> > self.params.add(arg)
> >
>
> Or even better (Python 2.5):
>
> class Foo(object):
> def __init__(self, *args):
> self.params = [arg if isinstance(arg, Bar) else Bar(arg) for
> arg in args]
>
Interesting, I'm not familiar with this idiom...
More information about the Python-list
mailing list