F
fdu.xiaojf
Hi all, I'm not skilled at programming, so sorry for my ignorance.
My questions:
(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
(2)
when to use class methods and when to use functions ?
In my opinion, both of class methods and functions have advantages and
disadvantages. I have to pass many arguments to a function, which is
annoying. When using class methods, the arguments can be stored as
attributes of the class, which is convenient for later use. But I have
to create an object in advance.
I have googled the web, but haven't found too much specific answers.
Can somebody kindly answer my questions or point me to the resources
available on the web ?
Thanks a lot.
My questions:
(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
(2)
when to use class methods and when to use functions ?
In my opinion, both of class methods and functions have advantages and
disadvantages. I have to pass many arguments to a function, which is
annoying. When using class methods, the arguments can be stored as
attributes of the class, which is convenient for later use. But I have
to create an object in advance.
I have googled the web, but haven't found too much specific answers.
Can somebody kindly answer my questions or point me to the resources
available on the web ?
Thanks a lot.