Re: Instance attributes vs method arguments

Discussion in 'Python' started by M.-A. Lemburg, Nov 25, 2008.

  1. On 2008-11-25 08:27, John O'Hagan wrote:
    >
    > Is it better to do this:
    >
    > class Class_a():
    > def __init__(self, args):
    > self.a = args.a
    > self.b = args.b
    > self.c = args.c
    > self.d = args.d
    > def method_ab(self):
    > return self.a + self.b
    > def method_cd(self):
    > return self.c + self.d
    >
    > or this:
    >
    > class Class_b():
    > def method_ab(self, args):
    > a = args.a
    > b = args.b
    > return a + b
    > def method_cd(self, args)
    > c = args.c
    > d = args.d
    > return c + d
    >
    > ?


    That depends entirely on what you intend to do with objects
    of Class_a and Class_b. In the first case, you are persisting
    the argument attributes in the object, in the second case,
    you are merely working on them - just like you would in a
    function.

    > Assuming we don't need access to the args from outside the class,
    > is there anything to be gained (or lost) by not initialising attributes that
    > won't be used unless particular methods are called?


    It is always good practice to provide default values for
    instance variables in the class definition, both to enhance
    readability and to allow adding documentation regarding
    the variables, e.g.

    class Class_a:

    # Foo bar
    a = None

    # Foo baz
    b = None

    ...

    --
    Marc-Andre Lemburg
    eGenix.com

    Professional Python Services directly from the Source (#1, Nov 25 2008)
    >>> Python/Zope Consulting and Support ... http://www.egenix.com/
    >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
    >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/

    ________________________________________________________________________
    2008-11-12: Released mxODBC.Connect 0.9.3 http://python.egenix.com/

    :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


    eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
    Registered at Amtsgericht Duesseldorf: HRB 46611
    M.-A. Lemburg, Nov 25, 2008
    #1
    1. Advertising

  2. M.-A. Lemburg

    Rafe Guest

    snip
    > It is always good practice to provide default values for
    > instance variables in the class definition, both to enhance
    > readability and to allow adding documentation regarding
    > the variables, e.g.
    >
    > class Class_a:
    >
    >    # Foo bar
    >    a = None
    >
    >    # Foo baz
    >    b = None

    snip

    Those are not instance 'variables' (attributes), they are class
    attributes. I used to do that in JScript, so I did it in python when I
    moved over. It caused a lot of trouble for me as a beginner. I was
    eventually given the advice to stop it. I haven't looked back since
    (until now). I think you would just be adding a new self.a which
    blocks access to your class.a, but I'm still shaky on this knowledge.

    Instance attribute defaults would be inside __init__() and before
    unpacking the *args.

    - Rafe
    Rafe, Nov 25, 2008
    #2
    1. Advertising

  3. M.-A. Lemburg a écrit :
    (snip)
    > It is always good practice to provide default values for
    > instance variables in the class definition, both to enhance
    > readability and to allow adding documentation regarding
    > the variables, e.g.


    Your opinion. As far as I'm concerned, using class variables this way is
    more of a WTF than a "good practice".

    > class Class_a:
    >
    > # Foo bar
    > a = None
    >
    > # Foo baz
    > b = None
    >
    > ...
    >
    Bruno Desthuilliers, Nov 25, 2008
    #3
  4. On Tue, 25 Nov 2008 10:21:18 +0100, M.-A. Lemburg wrote:

    > It is always good practice to provide default values for instance
    > variables in the class definition, both to enhance readability and to
    > allow adding documentation regarding the variables, e.g.
    >
    > class Class_a:
    >
    > # Foo bar
    > a = None
    >
    > # Foo baz
    > b = None


    "Always"?


    I would question that. If the instance attribute is always set, then the
    class attribute is just noise. It's not *wrong* exactly, but it is
    redundant. The documentation point is worthwhile, but I would say it is
    in the wrong place: it should be documented in the class docstring, not
    the source code.


    --
    Steven
    Steven D'Aprano, Nov 26, 2008
    #4
  5. M.-A. Lemburg

    Aahz Guest

    In article <>,
    M.-A. Lemburg <> wrote:
    >
    >It is always good practice to provide default values for instance
    >variables in the class definition, both to enhance readability and to
    >allow adding documentation regarding the variables, e.g.


    Actually, my company uses an occasional pattern of detecting whether an
    attribute has ever been set with hasattr(). I am not particularly fond
    of that mechanism because it has been the occasional source of subtle
    bugs, but I also see the utility.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "It is easier to optimize correct code than to correct optimized code."
    --Bill Harlan
    Aahz, Nov 26, 2008
    #5
    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. Gerard Flanagan
    Replies:
    3
    Views:
    443
    Terry Hancock
    Nov 19, 2005
  2. John O'Hagan

    Instance attributes vs method arguments

    John O'Hagan, Nov 25, 2008, in forum: Python
    Replies:
    8
    Views:
    293
    John O'Hagan
    Nov 26, 2008
  3. Raj Singh
    Replies:
    2
    Views:
    194
    Rick DeNatale
    May 29, 2008
  4. Greg Hauptmann
    Replies:
    9
    Views:
    241
    Loren Segal
    Jun 16, 2008
  5. Jayden
    Replies:
    16
    Views:
    510
    Steven D'Aprano
    Sep 29, 2012
Loading...

Share This Page