En Wed, 13 Jun 2007 22:03:50 -0300, Steven D'Aprano <[EMAIL PROTECTED]> escribió:
> On Wed, 13 Jun 2007 23:55:02 +0000, James Turk wrote: >>> James Turk wrote: >>> >>> > I have a situation where I have some class members that should only >>> be >>> > done once. Essentially my problem looks like this: >>> >> ChildClass doesn't take the parameter in it's constructor, it supplies >> it for the BaseClass. Every ChildClass of the same type should use >> the same dataset. > > Then each type of ChildClass should be a sub-class, and provide it's own > dataset: > > > class BaseClass: > dataset = None > # blah blah blah... > > > class ChildClass1(BaseClass): > dataset = SomethingUseful > class ChildClass2(BaseClass): > dataset = SomethingElse But the OP also stated that creating the dataset is expensive. The original code does what you say (each ChildClass is a subclass and provides its own dataset) so I think is an acceptable solution: py> def build_dataset(x): ... print "building dataset:",x ... return [x] ... py> class Base(object): ... dataset = None ... def __init__(self, param): ... if type(self).dataset is None: ... type(self).dataset = build_dataset(param) ... py> class ChildClass1(Base): ... def __init__(self): ... Base.__init__(self, "Params for ChildClass1") ... py> class AnotherChildClass(Base): ... def __init__(self): ... Base.__init__(self, "Params for AnotherChildClass") ... py> c1 = ChildClass1() building dataset: Params for ChildClass1 py> c2 = AnotherChildClass() building dataset: Params for AnotherChildClass py> c3 = ChildClass1() py> print Base.dataset None py> print ChildClass1.dataset ['Params for ChildClass1'] py> print AnotherChildClass.dataset ['Params for AnotherChildClass'] py> print c1.dataset ['Params for ChildClass1'] py> print c3.dataset ['Params for ChildClass1'] py> print c1.dataset is c3.dataset True py> -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list