a class variable question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • micklee74@hotmail.com

    #1

    a class variable question

    hi
    i have define a class like this

    class A:
    _var1 = 0
    def __init__(self):
    ## some initialization
    self.func1()

    def func1():
    .....
    _var1 = 1
    ....

    def getvarValue(sel f):
    return _var1
    I wanted var1 to be "global" inside Class A.
    when i do

    AnInstance = A()
    AnInstance.getv arValue()

    it gives me 0. It should be 1. What am i doing wrong here..thanks

  • Pierre Quentel

    #2
    Re: a class variable question

    In func1, _var1 = 1 creates a local variable _var1 (local to the
    method), not an attribute of the instance. If you want an instance
    attribute you must specify the reference to the instance by
    self._var1 = 1 ; self must be passed as an attribute to func1

    def func1(self):
    self._var1 = 1

    Similarly, in getvarValue :

    def getvarValue(sel f):
    return self._var1

    Pierre

    Comment

    • Marco Wahl

      #3
      Re: a class variable question

      Hi,

      just some lines added below. hth

      micklee74@hotma il.com wrote:[color=blue]
      > hi
      > i have define a class like this
      >
      > class A:
      > _var1 = 0
      > def __init__(self):
      > ## some initialization
      > self.func1()
      >
      > def func1():[/color]

      self
      [color=blue]
      > .....
      > _var1 = 1[/color]

      A._var1 = 1
      [color=blue]
      > ....
      >
      > def getvarValue(sel f):
      > return _var1[/color]

      return A._var1
      [color=blue]
      > I wanted var1 to be "global" inside Class A.
      > when i do
      >
      > AnInstance = A()
      > AnInstance.getv arValue()
      >
      > it gives me 0. It should be 1. What am i doing wrong here..thanks[/color]

      Comment

      • Erik Max Francis

        #4
        Re: a class variable question

        micklee74@hotma il.com wrote:
        [color=blue]
        > class A:
        > _var1 = 0
        > def __init__(self):
        > ## some initialization
        > self.func1()
        >
        > def func1():
        > .....
        > _var1 = 1
        > ....[/color]

        You mean::

        class A:
        _var1 = 0

        ...

        def func1(self):
        A._var1 = 1

        All you're doing in your example is setting a local variable inside the
        func1 method, which has no effect.

        --
        Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
        San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
        Every path has its puddle.
        -- (an English proverb)

        Comment

        • Erik Max Francis

          #5
          Re: a class variable question

          Pierre Quentel wrote:
          [color=blue]
          > In func1, _var1 = 1 creates a local variable _var1 (local to the
          > method), not an attribute of the instance. If you want an instance
          > attribute you must specify the reference to the instance by
          > self._var1 = 1 ; self must be passed as an attribute to func1
          >
          > def func1(self):
          > self._var1 = 1[/color]

          Note this only changes the attribute in the instance. If he wants it to
          be changed for all other instances, he needs to change it in the class
          with:: A._var1 = 1

          --
          Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
          San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
          Every path has its puddle.
          -- (an English proverb)

          Comment

          • Pierre Quentel

            #6
            Re: a class variable question


            Erik Max Francis wrote:[color=blue]
            > Note this only changes the attribute in the instance. If he wants it to
            > be changed for all other instances, he needs to change it in the class
            > with:: A._var1 = 1[/color]

            Yes, but in the OP's code func1() is called by __init__ for every
            instance - which in fact makes declaring _var1 as a class attribute
            useless

            Anyway, I find that changing the class attribute by calling a method on
            an instance is a little confusing. I would rather set the class
            attribute like this :

            class A2:

            _var1 = 0

            def getvarValue(sel f):
            return self._var1

            a = A2()
            print a.getvarValue()[color=blue][color=green][color=darkred]
            >>> 0[/color][/color][/color]
            A2._var1 = 0 # change class attribute
            print a.getvarValue()[color=blue][color=green][color=darkred]
            >>> 1[/color][/color][/color]
            b = A2()
            print b.getvarValue()[color=blue][color=green][color=darkred]
            >>> 1[/color][/color][/color]

            Or using a class method :

            class A3:

            _var1 = 0

            @classmethod
            def func1(cls):
            cls._var1 = 1

            def getvarValue(sel f):
            return self._var1

            a = A3()
            print a.getvarValue()[color=blue][color=green][color=darkred]
            >>> 0[/color][/color][/color]
            A3.func1() # change class attribute
            print a.getvarValue()[color=blue][color=green][color=darkred]
            >>> 1[/color][/color][/color]
            b = A3()
            print b.getvarValue()[color=blue][color=green][color=darkred]
            >>> 1[/color][/color][/color]

            Pierre

            Comment

            • Steve Holden

              #7
              Re: a class variable question

              Erik Max Francis wrote:
              [...][color=blue]
              > All you're doing in your example is setting a local variable inside the
              > func1 method, which has no effect.
              >[/color]
              I think EMF was thinking, but failed to write, "outside the function" at
              the end of that sentence ;-)

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC/Ltd http://www.holdenweb.com
              Love me, love my blog http://holdenweb.blogspot.com
              Recent Ramblings http://del.icio.us/steve.holden

              Comment

              • Bruno Desthuilliers

                #8
                Re: a class variable question

                micklee74@hotma il.com wrote:[color=blue]
                > hi
                > i have define a class like this
                >[/color]
                # class A:
                class A(object):[color=blue]
                > _var1 = 0
                > def __init__(self):
                > ## some initialization
                > self.func1()
                >[/color]
                # def func1():
                def func1(self):[color=blue]
                > .....[/color]
                # _var1 = 1
                self.__class__. _var1 = 1
                # or if func1() does'nt use any instance attributes:
                # @classmethod
                # def func1(cls):
                # ....
                # cls._var1 = 1
                [color=blue]
                > def getvarValue(sel f):[/color]
                # return _var1
                return self.__class__. _var1
                [color=blue]
                > I wanted var1 to be "global" inside Class A.[/color]

                The name is "class attribute".



                --
                bruno desthuilliers
                python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                p in 'onurb@xiludom. gro'.split('@')])"

                Comment

                Working...