Class level variables in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Munroe

    Class level variables in Python

    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:
    [color=blue][color=green][color=darkred]
    >>> class rectangle(objec t):[/color][/color][/color]
    z = 1
    def __init__(self):
    self.x = 2
    [color=blue][color=green][color=darkred]
    >>> r = rectangle()
    >>> print r.z[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> print r.x[/color][/color][/color]
    2[color=blue][color=green][color=darkred]
    >>> r.z = 16
    >>> print r.z[/color][/color][/color]
    16[color=blue][color=green][color=darkred]
    >>> r.x = 17
    >>> print r.x[/color][/color][/color]
    17[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    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
  • Terry Reedy

    #2
    Re: Class level variables in Python


    "Brian Munroe" <bmunroe@tribad or.nu> wrote in message
    news:c16d085c.0 308271543.3dfc5 [email protected] gle.com...[color=blue]
    > I am just starting to learn the OO side of Python scripting, and I[/color]
    am[color=blue]
    > a little confused on the following. Take the following example[/color]
    class:[color=blue]
    >[color=green][color=darkred]
    > >>> class rectangle(objec t):[/color][/color]
    > z = 1
    > def __init__(self):
    > self.x = 2
    >[color=green][color=darkred]
    > >>> r = rectangle()
    > >>> print r.z[/color][/color]
    > 1[color=green][color=darkred]
    > >>> print r.x[/color][/color]
    > 2[color=green][color=darkred]
    > >>> r.z = 16
    > >>> print r.z[/color][/color]
    > 16[color=green][color=darkred]
    > >>> r.x = 17
    > >>> print r.x[/color][/color]
    > 17[color=green][color=darkred]
    > >>>[/color][/color]
    >
    > 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)[/color]

    Everything set at 'top' level under the class statement is a class
    attribute. Ditto for anything set outside the class statement as
    someclass.attri bute. 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.at tribute 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


    Comment

    • Sean Ross

      #3
      Re: Class level variables in Python

      "Brian Munroe" <bmunroe@tribad or.nu> wrote in message
      news:c16d085c.0 308271543.3dfc5 [email protected] gle.com...[color=blue]
      > 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)[/color]

      Hi.
      Yes there is a difference. One is an instance attribute, the other is a
      class attribute:
      [color=blue][color=green][color=darkred]
      >>> class C:[/color][/color][/color]
      .... attr = 1
      .... def __init__(self):
      .... self.attr = 2
      ....[color=blue][color=green][color=darkred]
      >>> c = C()
      >>> print c.attr[/color][/color][/color]
      2[color=blue][color=green][color=darkred]
      >>> print c.__class__.att r[/color][/color][/color]
      1[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      HTH
      Sean


      Comment

      • mackstann

        #4
        Re: Class level variables in Python

        On Wed, Aug 27, 2003 at 04:43:03PM -0700, Brian Munroe wrote:[color=blue]
        > 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)[/color]

        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.specialSeq uence:
        # .. 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

        Comment

        Working...