Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > questions about programming styles

Reply
Thread Tools

questions about programming styles

 
 
fdu.xiaojf@gmail.com
Guest
Posts: n/a
 
      05-20-2007
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.
 
Reply With Quote
 
 
 
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      05-20-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
Ben C
Guest
Posts: n/a
 
      05-20-2007
On 2007-05-20, (E-Mail Removed) <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Michael Hoffman
Guest
Posts: n/a
 
      05-20-2007
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
programming styles Amali Java 16 02-07-2007 01:14 PM
Styles of programming Thomas Korsgaard Java 5 12-11-2005 11:56 PM
Ruby programming styles and new program? Bill Ruby 16 10-17-2004 12:35 AM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM
(Programming) Various Syntax styles Chris Mantoulidis C++ 18 12-15-2003 09:09 AM



Advertisments