questions about programming styles

Discussion in 'Python' started by fdu.xiaojf@gmail.com, May 20, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. 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.
    Bjoern Schliessmann, May 20, 2007
    #2
    1. Advertising

  3. Ben C Guest

    On 2007-05-20, <> 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.
    Ben C, May 20, 2007
    #3
  4. 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
    Michael Hoffman, May 20, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil S.

    Styles missing after upgrade

    Phil S., Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    308
    Phil S.
    Jul 8, 2003
  2. Thomas Korsgaard

    Styles of programming

    Thomas Korsgaard, Dec 11, 2005, in forum: Java
    Replies:
    5
    Views:
    352
  3. Chris Mantoulidis

    (Programming) Various Syntax styles

    Chris Mantoulidis, Dec 11, 2003, in forum: C++
    Replies:
    18
    Views:
    654
    Nils Petter Vaskinn
    Dec 15, 2003
  4. Amali

    programming styles

    Amali, Feb 5, 2007, in forum: Java
    Replies:
    16
    Views:
    463
    MikeNereson
    Feb 7, 2007
  5. Bill
    Replies:
    16
    Views:
    219
    David Ross
    Oct 17, 2004
Loading...

Share This Page