Accessing class variable at class creation time

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

    #1

    Accessing class variable at class creation time

    Hi!

    class A:
    X = 2
    def F():
    print A.X
    F()

    The above fails because the name A is not
    yet at global scope when the reference A.X
    is reached. Is there any way to refer to A.X
    without making explicit use of the name 'A'?
    Admittedly the code looks pretty unusual,
    but I'm defining configuration "minilangua ges"
    to configure different parts of an application,
    each inside a configuration class (like A),
    and I find this very convenient except for
    the above problem.

    Thank you in advance
    Regards,
    Carlos

  • Jan-Ole Esleben

    #2
    Re: Accessing class variable at class creation time

    You could use self.__class__. X

    HTH, Ole


    23 Sep 2005 14:01:21 -0700, Carlos <carlosjosepita @gmail.com>:[color=blue]
    > Hi!
    >
    > class A:
    > X = 2
    > def F():
    > print A.X
    > F()
    >
    > The above fails because the name A is not
    > yet at global scope when the reference A.X
    > is reached. Is there any way to refer to A.X
    > without making explicit use of the name 'A'?
    > Admittedly the code looks pretty unusual,
    > but I'm defining configuration "minilangua ges"
    > to configure different parts of an application,
    > each inside a configuration class (like A),
    > and I find this very convenient except for
    > the above problem.
    >
    > Thank you in advance
    > Regards,
    > Carlos
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >[/color]

    Comment

    • Simon Percivall

      #3
      Re: Accessing class variable at class creation time

      It might be that I'm complicating something easy here, but I
      immediately thought of

      import sys

      class A:
      X = 2
      def F():
      f = sys._getframe() .f_back
      print f.f_locals["X"]
      F()

      Comment

      • Benji York

        #4
        Re: Accessing class variable at class creation time

        Carlos wrote:[color=blue]
        > Hi!
        >
        > class A:
        > X = 2
        > def F():
        > print A.X
        > F()
        >
        > The above fails because the name A is not
        > yet at global scope when the reference A.X
        > is reached. Is there any way to refer to A.X
        > without making explicit use of the name 'A'?[/color]

        How about this:
        [color=blue][color=green][color=darkred]
        >>> class A:[/color][/color][/color]
        .... X = 2
        .... print X
        ....
        2
        --
        Benji York

        Comment

        • Jan-Ole Esleben

          #5
          Re: Accessing class variable at class creation time

          That doesn't really give him a way of using the class variable inside a method.

          Ole


          2005/9/24, Benji York <benji@benjiyor k.com>:[color=blue]
          > Carlos wrote:[color=green]
          > > Hi!
          > >
          > > class A:
          > > X = 2
          > > def F():
          > > print A.X
          > > F()
          > >
          > > The above fails because the name A is not
          > > yet at global scope when the reference A.X
          > > is reached. Is there any way to refer to A.X
          > > without making explicit use of the name 'A'?[/color]
          >
          > How about this:
          >[color=green][color=darkred]
          > >>> class A:[/color][/color]
          > ... X = 2
          > ... print X
          > ...
          > 2
          > --
          > Benji York
          >
          > --
          > http://mail.python.org/mailman/listinfo/python-list
          >[/color]

          Comment

          • Benji York

            #6
            Re: Accessing class variable at class creation time

            Jan-Ole Esleben wrote:[color=blue]
            > That doesn't really give him a way of using the class variable inside a method.[/color]

            Oh! I must have misunderstood the question. I'd like to know more
            about why the OP wants to do this; a small example would help narrow
            down the possibilities.
            --
            Benji York

            Comment

            • Carlos

              #7
              Re: Accessing class variable at class creation time

              Thank you all!

              After all, I found at least three more or less convenient alternatives:

              1) Pass X as default parameter to F.
              2) Set globals() from inside A, something like globals()['A_locals'] =
              locals() or globals()['A_X'] = X. Then access A_locals or A_X from F.
              3) Use sys._getframe(1 ) or sys._getframe() .f_back to get the caller
              frame.

              But the following won't work:
              [color=blue]
              > self.__class__. X[/color]

              Because there is no self around here, I'm not trying to access X from a
              class instance but instead from code running at class definition time.
              [color=blue]
              > class A:
              > ... X = 2
              > ... print X[/color]

              Of course this will print X but the point was to do it from inside a
              function.

              Regards,
              Carlos

              Comment

              • Dave Hansen

                #8
                Re: Accessing class variable at class creation time

                On 23 Sep 2005 14:01:21 -0700, "Carlos" <carlosjosepita @gmail.com>
                wrote:
                [color=blue]
                >Hi!
                >
                >class A:
                > X = 2
                > def F():
                > print A.X
                > F()
                >
                >The above fails because the name A is not
                >yet at global scope when the reference A.X[/color]

                Maybe I'm missing something. Python 2.4.1#65 under Win32 Idle 1.1.1
                gives me the following:

                --begin included file---[color=blue][color=green][color=darkred]
                >>> class A:[/color][/color][/color]
                X = 2
                def F():
                print A.X
                F()


                2
                ---end included file---

                But what good is F? You can't call A.F() because methods need an
                instance, and calling A.F(instance) or instance.F() throws a TypeError
                (argument count) exception.

                Regards,

                -=Dave
                --
                Change is inevitable, progress is not.

                Comment

                Working...