473,554 Members | 2,806 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 1175
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
2934
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...
18
1758
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
2561
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
1452
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...
8
1342
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,...
3
1359
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
2485
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
2185
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
3640
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...
0
7579
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...
0
7499
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...
0
7780
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8013
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7531
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7862
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...
0
5136
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...
0
3530
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
814
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...

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.