'Once' properties.
Carl Banks
pavlovevidence at gmail.com
Tue Oct 6 02:12:46 EDT 2009
On Oct 5, 11:07 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
> On Oct 5, 7:56 pm, menomnon <p... at well.com> wrote:
> > Does python have a ‘once’ (per class) feature?
>
> > ‘Once’, as I’ve know it is in Eiffel. May be in Java don’t.
>
> > The first time you instantiate a given class into an object it
> > constructs, say, a dictionary containing static information. In my
> > case static is information that may change once a week at the most and
> > there’s no need to be refreshing this data during a single running of
> > the program (currently maybe 30 minutes).
>
> > So you instantiate the same class into a second object, but instead of
> > going to the databases again and recreating the same dictionary a
> > second time, you get a pointer or reference to the one already created
> > in the first object – copies into the second object that is. And the
> > dictionary, no matter how many instances of the object you make, is
> > always the same one from the first object.
>
> > So, as we put it, once per class and not object.
>
> > Saves on both time and space.
>
> Sounds like Borg Pattern:
>
> http://code.activestate.com/recipes/66531/
BTW, for your problem you'd probably want to add some kind of
conditional initialization code:
class Borg(object):
_shared_state = { 'initialized' : False }
def __init__(self):
self.__dict__ = self._shared_state
if self.initialized:
return
# perform initialization here
self.initialized = True
Carl Banks
More information about the Python-list
mailing list