Style question - defining immutable class data members

Discussion in 'Python' started by Maxim Khitrov, Mar 14, 2009.

  1. Very simple question on the preferred coding style. I frequently write
    classes that have some data members initialized to immutable values.
    For example:

    class Test(object):
    def __init__(self):
    self.some_value = 0
    self.another_value = None

    Similar effect can be achieved by defining some_value and
    another_value for the entire class, like so:

    class Test(object):
    some_value = 0
    another_value = None

    The advantage of doing this is that the assignments are evaluated once
    and thus the creation of that class is a bit faster. Access is still
    performed through self.some_value and self.another_value. Is there a
    reason to prefer the first style over the second?

    - Max
    Maxim Khitrov, Mar 14, 2009
    #1
    1. Advertising

  2. Maxim Khitrov

    Aahz Guest

    In article <>,
    Maxim Khitrov <> wrote:
    >
    >Very simple question on the preferred coding style. I frequently write
    >classes that have some data members initialized to immutable values.
    >For example:
    >
    >class Test(object):
    > def __init__(self):
    > self.some_value = 0
    > self.another_value = None
    >
    >Similar effect can be achieved by defining some_value and
    >another_value for the entire class, like so:
    >
    >class Test(object):
    > some_value = 0
    > another_value = None
    >
    >The advantage of doing this is that the assignments are evaluated once
    >and thus the creation of that class is a bit faster. Access is still
    >performed through self.some_value and self.another_value. Is there a
    >reason to prefer the first style over the second?


    Well, as you can see from reading this whole thread, it can be the source
    of some confusion. Nevertheless, I personally sometimes use the style of
    initializing at the class level. I think it's probably worth creating a
    style guide entry for this issue if you're using Python for your
    employer.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "At Resolver we've found it useful to short-circuit any doubt and just
    refer to comments in code as 'lies'. :)"
    --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-3-22
    Aahz, Mar 24, 2009
    #2
    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. CoolPint
    Replies:
    8
    Views:
    955
    Jeff Schwab
    Dec 14, 2003
  2. chuck
    Replies:
    3
    Views:
    483
    Juha Nieminen
    Mar 30, 2008
  3. Gary Herron
    Replies:
    3
    Views:
    632
    Gary Herron
    Mar 14, 2009
  4. Gary Herron
    Replies:
    5
    Views:
    217
    R. David Murray
    Mar 15, 2009
  5. John Posner
    Replies:
    7
    Views:
    229
    Aaron Brady
    Mar 16, 2009
Loading...

Share This Page