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
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)
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
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
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
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
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
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
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
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
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
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
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):
13 matches
Mail list logo