Proper class initialization

Christoph Zwerschke cito at online.de
Fri Mar 3 21:27:55 CET 2006


gry at 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 that, Steven's second suggestion was better:

class A:
     def calculate_sum():
         do_stuff
         return sum_value
     sum = calculate_sum()

That's not only easier simpler, it also avoids calling calculate_sum() 
every time you create an instance.

-- Christoph




More information about the Python-list mailing list