Re: Proper class initialization

2006-03-03 Thread Christoph Zwerschke
gry@ll.mit.edu schrieb: > Hmm, the meta-class hacks mentioned are cool, but for this simple a > case how about just: > > class A: >def __init__(self): > self.__class__.sum = self.calculate_sum() >def calculate_sum(self): > do_stuff > return sum_value If you do it like th

Re: Proper class initialization

2006-03-02 Thread Steven Bethard
Kent Johnson wrote: > Steven Bethard wrote: >> I don't run into this often, but when I do, I usually go Jack >> Diederich's route:: >> >> class A(object): >> class __metaclass__(type): >> def __init__(cls, name, bases, classdict): >> cls.sum = sum(xrange(10)

Re: Proper class initialization

2006-03-02 Thread gry
Christoph Zwerschke wrote: > Usually, you initialize class variables like that: > > class A: > sum = 45 > > But what is the proper way to initialize class variables if they are the > result of some computation or processing as in the following silly > example (representative for more: > > class

Re: Proper class initialization

2006-03-02 Thread Kent Johnson
Steven Bethard wrote: > I don't run into this often, but when I do, I usually go Jack > Diederich's route:: > > class A(object): > class __metaclass__(type): > def __init__(cls, name, bases, classdict): > cls.sum = sum(xrange(10)) I think you should call t

Re: Proper class initialization

2006-03-02 Thread Christoph Zwerschke
Steven Bethard wrote: > I don't run into this often, but when I do, I usually go Jack > Diederich's route:: > > class A(object): > class __metaclass__(type): > def __init__(cls, name, bases, classdict): > cls.sum = sum(xrange(10)) Good idea, that is really

Re: Proper class initialization

2006-03-02 Thread Christoph Zwerschke
Steven Bethard wrote: > I assume the intention was to indicate that the initialization required > multiple statements. I just couldn't bring myself to write that > horrible for-loop when the sum() function is builtin. ;) Yes, this was just dummy code standing for something that really requires

Re: Proper class initialization

2006-03-01 Thread Steven Bethard
Leif K-Brooks wrote: > Steven Bethard wrote: >> class A(object): >> def _get_sum(): >> return sum(xrange(10)) >> sum = _get_sum() > > What's wrong with sum = sum(xrange(10))? Nothing, except that it probably doesn't answer the OP's question. The OP presented a "s

Re: Proper class initialization

2006-03-01 Thread Leif K-Brooks
Steven Bethard wrote: > class A(object): > def _get_sum(): > return sum(xrange(10)) > sum = _get_sum() What's wrong with sum = sum(xrange(10))? -- http://mail.python.org/mailman/listinfo/python-list

Re: Proper class initialization

2006-03-01 Thread Steven Bethard
Christoph Zwerschke wrote: > But I wonder whether it is possible to put all this init code into one > class initialization method, something like that: > > class A: > > @classmethod > def init_class(self): > sum = 0 > for i in range(10): > sum += i > s

Re: Proper class initialization

2006-03-01 Thread Christoph Zwerschke
Jack Diederich wrote: > ... __metaclass__ = MyMeta Thanks. I was not aware of the __metaclass__ attribute. Still a bit complicated and as you said, difficult to read, as the other workarounds already proposed. Anyway, this is probably not needed so often. -- Christoph -- http://mail.python.o

Re: Proper class initialization

2006-03-01 Thread Larry Bates
Christoph Zwerschke wrote: > Usually, you initialize class variables like that: > > class A: >sum = 45 > > But what is the proper way to initialize class variables if they are the > result of some computation or processing as in the following silly > example (representative for more: > > cla

Re: Proper class initialization

2006-03-01 Thread Jack Diederich
On Wed, Mar 01, 2006 at 09:25:36PM +0100, Christoph Zwerschke wrote: > Usually, you initialize class variables like that: > > class A: > sum = 45 > > But what is the proper way to initialize class variables if they are the > result of some computation or processing as in the following silly

Proper class initialization

2006-03-01 Thread Christoph Zwerschke
Usually, you initialize class variables like that: class A: sum = 45 But what is the proper way to initialize class variables if they are the result of some computation or processing as in the following silly example (representative for more: class A: sum = 0 for i in range(10):