questions about programming styles

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.
 
B

Bjoern Schliessmann

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
 
B

Ben C

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.
 
M

Michael Hoffman

(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().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top