473,654 Members | 3,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_valu e
and then if the vlue of attribute is needed,
self.cal_attr(a rgs)
some_var = self.attr
or I can define cal_attr() as follows:
(B)
def cal_attr(self, args):
#do some calculations
return calculated_valu e
and then, if the value of attribute is needed,
self.attr = self.cal_attr(a rgs)
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 1181
fd********@gmai l.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_valu e
and then if the vlue of attribute is needed,
self.cal_attr(a rgs)
some_var = self.attr
or I can define cal_attr() as follows:
(B)
def cal_attr(self, args):
#do some calculations
return calculated_valu e
and then, if the value of attribute is needed,
self.attr = self.cal_attr(a rgs)
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********@gmai l.com <fd********@gma il.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_valu e
and then if the vlue of attribute is needed,
self.cal_attr(a rgs)
some_var = self.attr
or I can define cal_attr() as follows:
(B)
def cal_attr(self, args):
#do some calculations
return calculated_valu e
and then, if the value of attribute is needed,
self.attr = self.cal_attr(a rgs)
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(a rgs)

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********@gmai l.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_valu e
and then if the vlue of attribute is needed,
self.cal_attr(a rgs)
some_var = self.attr
or I can define cal_attr() as follows:
(B)
def cal_attr(self, args):
#do some calculations
return calculated_valu e
and then, if the value of attribute is needed,
self.attr = self.cal_attr(a rgs)
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_valu e

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
2941
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 happy to hear your advice on a couple of practicalities. 1. media="all" required? In the <head> of my pages, I have the following: <link rel="stylesheet" href="style.css" type="text/css"></link>
18
1765
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
2571
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 programming skills and development strategies
4
1457
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 studying "Programming C#" by Jesse Liberty at home in the evening and "Inside C#" at work (I go slowly because I also have to work, but that's better than nothing). I am sure they are two good books and I understand them without many troubles. What...
8
1344
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 horizontal scrollbars are no good but I would rather have them than having the page display in an awkward, uncontrollable way. As I understand, designers used to go the monkey way and just have a line as a picture with a certain width to force this...
3
1363
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 this list? Scott Huey
39
2503
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 Bar(int i) const; } (I said that since Bar is a static method, it can only access
11
2191
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;} #parent_block {background-color: red; width: 100%;} #nested_child_block {margin: 150px 0px; background-color: green; color: white;}
22
3655
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 than just family and friends. Either way, I'm planning on this site blowing up once I have enough of a feature set, so I'm concerned about performance and scalability in the long run. I've worked for a software company, but I've never...
0
8372
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8285
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8591
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7304
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.