By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,551 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

questions about programming styles

P: n/a
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.
May 20 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
fd********@gmail.com wrote:
Hi all, I'm not skilled at programming, so sorry for my ignorance.
?!

Seems you met many not-so-nice programmers.
(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
Do you mean when to save a class attribute to an instance or how to
calculate and save something _inside_ an instance? No matter what,
it depends largely on your design. If some value is a
permanent "feature" of an instance, it makes sense to save it as a
member. If the value must always be calculated on the fly, it makes
sense to use a function that returns it. Also check the docs on
Python's "property" feature.
(2)
when to use class methods and when to use functions ?
Functions are best used if you just "do" something that isn't
focused on a specific class or object type.

Class methods are best used if there is something to "do" that
always needs the class object.
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.
Yep. But there are also recipes to let functions store and reuse
parameters, I think.
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.
But only a class object, which exists when you have defined a class.
Class methods don't operate on instances.

All you asked depends much on specific design. So if you provide
some details, I'm sure at least one here will have a hint.

Regards,
Björn

--
BOFH excuse #208:

Your mail is being routed through Germany ... and they're censoring
us.

May 20 '07 #2

P: n/a
On 2007-05-20, fd********@gmail.com <fd********@gmail.comwrote:
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
It looks from your example like this attr depends on the args passed to
cal_attr. Is it really then an "attribute" of the object, or just the
result of a calculation that the object provides? If the latter, you
might not want the variable self.attr at all, and just write

some_var = self.cal_attr(args)

Otherwise self.attr just ends up storing the result of the previous call
to cal_attr. Is that useful? Does any other part of the program actually
need that? If not don't store it.
(2)
when to use class methods and when to use functions ?
I think you just mean methods (Python has something special called
"class methods" which are for, er, well, you almost never need them).
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.
That's about right. There's no hard and fast rule. If you need those
values again it may be worth storing them, but be wary of storing
computed values if there's a chance they're going to get out of date.
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 ?
You're asking good questions and they don't have easy answers.
May 20 '07 #3

P: n/a
fd********@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
May 20 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.