questions about programming styles
Michael Hoffman
cam.ac.uk at mh391.invalid
Sun May 20 06:46:47 EDT 2007
fdu.xiaojf at gmail.com wrote:
> (1)
> which is the better way to calculate the value of attributes of a class ?
> for example:
>
> (A)
> def cal_attr(self, args):
> #do some calculations
> self.attr = calculated_value
> and then if the vlue of attribute is needed,
> self.cal_attr(args)
> some_var = self.attr
> or I can define cal_attr() as follows:
> (B)
> def cal_attr(self, args):
> #do some calculations
> return calculated_value
> and then, if the value of attribute is needed,
> self.attr = self.cal_attr(args)
> some_var = self.attr
In many cases (I would really have to see the context to be sure) would
prefer something like:
def get_attr(self, args):
# calculations here
return calculated_value
Don't have a self.attr, just return the results of get_attr().
--
Michael Hoffman
More information about the Python-list
mailing list