473,418 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

questions about programming styles

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
3 1166
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Garmt de Vries | last post by:
For a long time, I've used CSS to style my webpages, but only for media "screen" and "print". Now I've started looking into styling them for other media like "projection" and "handheld". I'd be...
18
by: Chris Mantoulidis | last post by:
There is a LARGE number of syntax styles in most (if not all) programming languages. For example, one syntax style (my current one): .... int main() { for (int i = 0; i < 50; i++) {
3
by: Raed Sawalha | last post by:
I've been developing since year ago , I noticed that I developed my programming skills in first 6 months then , my coding style and strategy been the same, so what you advice me to develop my...
4
by: Java Challenge | last post by:
I am trying to work hard to become a programmer and eventually get a job as a programmer, I have a low paying job at the moment as technical support and a family to maintain. 1 - I started to...
8
by: onetitfemme | last post by:
1._ How can you stop a browser from displaying horizontally floating objects in new lines once they should not be shrunk any longer? The browser should then show a horizontal scrollbar. I know...
3
by: Redefined Horizons | last post by:
I've got a third-part application that exposes a C API. I'd like to wrap it in Python. Is there a specific forum that covers extending and embedding Python, or are those type of questions O.K. on...
39
by: Digital Puer | last post by:
I'm not the world's greatest C++ programmer, so I had a hard time with these. Some help would be appreciated. 1. Comment on the declaration of function Bar() below: class Foo { static int...
11
by: Gérard Talbot | last post by:
Hello, <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <title></title> <style type="text/css"> body {background-color: white; color: black;}...
22
by: Jesse Burns | last post by:
I'm about to start working on my first large scale site (in my opinion) that will hopefully have 1000+ users a day. ok, this isn't on the google/facebook scale, but it's going to be have more hits...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.