Re: 'Once' properties.

2009-10-06 Thread Carl Banks
On Oct 5, 7:56 pm, menomnon p...@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/


Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 'Once' properties.

2009-10-06 Thread Carl Banks
On Oct 5, 11:07 pm, Carl Banks pavlovevide...@gmail.com wrote:
 On Oct 5, 7:56 pm, menomnon p...@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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 'Once' properties.

2009-10-06 Thread Scott David Daniels

menomnon 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.

Look into metaclasses:

class MyType(type):
def __new__(class_, name, bases, dct):
result = type.__new__(class_, name, bases, dct)
result._init_class()
return result

class ClassBase(object):
__metaclass__ = MyType

@classmethod
def _init_class(class_):
print 'initializing class'


class Initialized(ClassBase):
@classmethod
def _init_class(class_):
class_.a, class_.b = 1, 2
super(Initialized, class_)._init_class()

print Initialized.a, Initialized.b

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: 'Once' properties.

2009-10-06 Thread Scott David Daniels

Scott David Daniels wrote:

...
Look into metaclasses:

...

class Initialized(ClassBase):
@classmethod
def _init_class(class_):
class_.a, class_.b = 1, 2
super(Initialized, class_)._init_class()


Mea culpa:  Here super is _not_ a good idea, and I had tried that
and recoded, but cut and pasted the wrong code.  I just noticed
that I had done so this morning.

class Initialized(ClassBase):
@classmethod
def _init_class(class_):
class_.a, class_.b = 1, 2
ClassBase._init_class()

print Initialized.a, Initialized.b

Much better.  There is probably a way to get to the MRO, but for now,
this should do.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: 'Once' properties.

2009-10-05 Thread Chris Rebert
On Mon, Oct 5, 2009 at 7:56 PM, menomnon p...@well.com wrote:
 Does python have a ‘once’ (per class) feature?

In Python, `class` is an executable statement, so you can put whatever
code you want in the class body (along with your method definitions)
and it will be run exactly once, at the time the class is defined
(that is, when the `class` statement is encountered by the
interpreter). In this way, you could create class variables containing
such static information.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list