Class level variables in Python

Discussion in 'Python' started by Brian Munroe, Aug 28, 2003.

  1. Brian Munroe

    Brian Munroe Guest

    I am just starting to learn the OO side of Python scripting, and I am
    a little confused on the following. Take the following example class:

    >>> class rectangle(object):

    z = 1
    def __init__(self):
    self.x = 2

    >>> r = rectangle()
    >>> print r.z

    1
    >>> print r.x

    2
    >>> r.z = 16
    >>> print r.z

    16
    >>> r.x = 17
    >>> print r.x

    17
    >>>


    I was wondering if someone could explain if there is any difference
    between initalizing your object attributes up in the __init__
    constructor or setting them up as (I am guessing at the name here)
    object level variables (like z)

    thanks

    -- brian
    Brian Munroe, Aug 28, 2003
    #1
    1. Advertising

  2. Brian Munroe

    mackstann Guest

    On Wed, Aug 27, 2003 at 04:43:03PM -0700, Brian Munroe wrote:
    > I was wondering if someone could explain if there is any difference
    > between initalizing your object attributes up in the __init__
    > constructor or setting them up as (I am guessing at the name here)
    > object level variables (like z)


    Generally, if something is more or less constant, I make it a class
    variable. If its value depends on the arguments passed to __init__, or
    if it is something like a network connection, file operation, etc, then
    it goes in __init__.

    class Foo:
    specialSequence = "blahblah"

    def __init__(self, filename):
    self.fp = file(filename, "r")
    if self.fp.read(8) == self.specialSequence:
    # .. do something ..

    specialSequence has no reason to be in the constructor, but fp does. I
    suppose that's a good guideline - if you can't think of a reason for it
    to be in __init__, then don't put it there.

    --
    m a c k s t a n n mack @ incise.org http://incise.org
    The four building blocks of the universe are fire, water, gravel and vinyl.
    -- Dave Barry
    mackstann, Aug 28, 2003
    #2
    1. Advertising

  3. Brian Munroe

    Sean Ross Guest

    "Brian Munroe" <> wrote in message
    news:...
    > I was wondering if someone could explain if there is any difference
    > between initalizing your object attributes up in the __init__
    > constructor or setting them up as (I am guessing at the name here)
    > object level variables (like z)


    Hi.
    Yes there is a difference. One is an instance attribute, the other is a
    class attribute:

    >>> class C:

    .... attr = 1
    .... def __init__(self):
    .... self.attr = 2
    ....
    >>> c = C()
    >>> print c.attr

    2
    >>> print c.__class__.attr

    1
    >>>


    HTH
    Sean
    Sean Ross, Aug 28, 2003
    #3
  4. Brian Munroe

    Terry Reedy Guest

    "Brian Munroe" <> wrote in message
    news:...
    > I am just starting to learn the OO side of Python scripting, and I

    am
    > a little confused on the following. Take the following example

    class:
    >
    > >>> class rectangle(object):

    > z = 1
    > def __init__(self):
    > self.x = 2
    >
    > >>> r = rectangle()
    > >>> print r.z

    > 1
    > >>> print r.x

    > 2
    > >>> r.z = 16
    > >>> print r.z

    > 16
    > >>> r.x = 17
    > >>> print r.x

    > 17
    > >>>

    >
    > I was wondering if someone could explain if there is any difference
    > between initalizing your object attributes up in the __init__
    > constructor or setting them up as (I am guessing at the name here)
    > object level variables (like z)


    Everything set at 'top' level under the class statement is a class
    attribute. Ditto for anything set outside the class statement as
    someclass.attribute. This include instance methods, which are common
    to all instances and therefore *attributes* of the class.

    Everything set within instance methods as self.attribute or outside as
    someinstance.attribute are instance attributes private to that
    instance. Just as a function can have a private local variable with
    the same name as a 'public' global variable, an instance can have an
    attribute of the same name as an attribute of its class. Just as
    function locals 'mask' the global of the same name, instance 'locals'
    usually* mask the class attribute of the same name.

    In your example above, you start with class attribute z and later add
    an r instance attribute of same name (but different value). First you
    see one, then the other.

    (* I believe the masking exception alluded to above has something to
    do with special methods, descriptors, and classes derived from
    builtins, but I do not know the current rule will enough to even quote
    it. But beginners usually need not worry about it.)

    Terry J. Reedy
    Terry Reedy, Aug 28, 2003
    #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. Landley

    Class-Level Variables in Web Form

    Landley, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,302
    Landley
    Sep 20, 2005
  2. pabbu
    Replies:
    8
    Views:
    707
    Marc Boyer
    Nov 7, 2005
  3. John Nagle
    Replies:
    22
    Views:
    616
    Gregory Ewing
    Oct 16, 2010
  4. John W. Long

    Class-Level Instance Variables

    John W. Long, Sep 23, 2006, in forum: Ruby
    Replies:
    1
    Views:
    117
    Devin Mullins
    Sep 23, 2006
  5. Phrogz
    Replies:
    4
    Views:
    109
    Trans
    Oct 26, 2006
Loading...

Share This Page