namespace question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jm.suresh@no.spam.gmail.com

    #1

    namespace question

    Hi ,

    class Test:
    a = 1
    b = 2
    c = 1+2

    Now, all a,b and c would be directly visible to the user from the
    instance of Test. I am looking for a way to make only c directly
    visible from the instance.


    --
    Suresh

  • Marc 'BlackJack' Rintsch

    #2
    Re: namespace question

    In <1165904406.053 883.129610@l12g 2000cwl.googleg roups.com>,
    jm.suresh@no.sp am.gmail.com wrote:
    class Test:
    a = 1
    b = 2
    c = 1+2
    >
    Now, all a,b and c would be directly visible to the user from the
    instance of Test. I am looking for a way to make only c directly
    visible from the instance.
    Simplest way:

    class Test:
    c = 3

    :-)

    You know that `a`, `b` and `c` are class variables and not instance
    variables!?

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • jm.suresh@no.spam.gmail.com

      #3
      Re: namespace question


      Marc 'BlackJack' Rintsch wrote:
      In <1165904406.053 883.129610@l12g 2000cwl.googleg roups.com>,
      jm.suresh@no.sp am.gmail.com wrote:
      >
      class Test:
      a = 1
      b = 2
      c = 1+2

      Now, all a,b and c would be directly visible to the user from the
      instance of Test. I am looking for a way to make only c directly
      visible from the instance.
      >
      Simplest way:
      >
      class Test:
      c = 3
      >
      :-)
      >
      You know that `a`, `b` and `c` are class variables and not instance
      variables!?
      Yes. I want to have only one class variable called c and a and b are
      required as temporary variables to calculate the value for c.

      I just found one way:
      class Test:
      a = 1
      b = 2
      c = a + b
      del a,b

      This one works. But I suppose there must be a way to artificially
      create a new block of code, some thing like this,

      class Test:
      c = None
      <<howToCreateAN ewNameSpace>>:
      # Objects created here are local to this scope
      a = 1
      b = 2
      global c
      c = a + b
      >
      Ciao,
      Marc 'BlackJack' Rintsch

      Comment

      • Fredrik Lundh

        #4
        Re: namespace question

        jm.suresh@no.sp am.gmail.com wrote:
        This one works. But I suppose there must be a way to artificially
        create a new block of code, some thing like this,
        >
        class Test:
        c = None
        <<howToCreateAN ewNameSpace>>:
        # Objects created here are local to this scope
        a = 1
        b = 2
        global c
        c = a + b
        if you want a local scope, use a function:

        class Test:
        c = do_calculation( 1, 2)

        </F>

        Comment

        • Piet van Oostrum

          #5
          Re: namespace question

          >>>>"jm.suresh@ no.spam.gmail.c om" <jm.suresh@gmai l.com(jssgc) wrote:
          >jssgcThis one works. But I suppose there must be a way to artificially
          >jssgccreate a new block of code, some thing like this,
          >jssgcclass Test:
          >jssgc c = None
          >jssgc <<howToCreateAN ewNameSpace>>:
          >jssgc # Objects created here are local to this scope
          >jssgc a = 1
          >jssgc b = 2
          >jssgc global c
          >jssgc c = a + b
          As you want c to be an *instance* variable, the normal idiom would be:

          class Test:
          def __init__(self):
          a = 1
          b = 2
          self.c = a+b

          x = Test()
          print x.c
          --
          Piet van Oostrum <piet@cs.uu.n l>
          URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
          Private email: piet@vanoostrum .org

          Comment

          • Paul Boddie

            #6
            Re: namespace question

            jm.suresh@no.sp am.gmail.com wrote:
            Yes. I want to have only one class variable called c and a and b are
            required as temporary variables to calculate the value for c.
            >
            I just found one way:
            class Test:
            a = 1
            b = 2
            c = a + b
            del a,b
            Or even...

            a = 1
            b = 2
            class Test:
            c = a + b

            Or even the apparently nonsensical...

            a = 1
            b = 2
            c = a + b
            class Test:
            c = c

            Insert del statements to remove module globals where appropriate.

            Paul

            Comment

            Working...