namespace question

Discussion in 'Python' started by jm.suresh@no.spam.gmail.com, Dec 12, 2006.

  1. Guest

    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
     
    , Dec 12, 2006
    #1
    1. Advertising

  2. In <>,
    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
     
    Marc 'BlackJack' Rintsch, Dec 12, 2006
    #2
    1. Advertising

  3. Guest

    Marc 'BlackJack' Rintsch wrote:
    > In <>,
    > 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
    <<howToCreateANewNameSpace>>:
    # Objects created here are local to this scope
    a = 1
    b = 2
    global c
    c = a + b

    >
    > Ciao,
    > Marc 'BlackJack' Rintsch
     
    , Dec 12, 2006
    #3
  4. 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
    > <<howToCreateANewNameSpace>>:
    > # 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>
     
    Fredrik Lundh, Dec 12, 2006
    #4
  5. >>>>> "" <> (jssgc) wrote:
    >jssgc> This one works. But I suppose there must be a way to artificially
    >jssgc> create a new block of code, some thing like this,


    >jssgc> class Test:
    >jssgc> c = None
    >jssgc> <<howToCreateANewNameSpace>>:
    >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 <>
    URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Dec 13, 2006
    #5
  6. Paul Boddie Guest

    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
     
    Paul Boddie, Dec 13, 2006
    #6
    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. Èý¹â
    Replies:
    1
    Views:
    585
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,146
  3. Anonymous
    Replies:
    3
    Views:
    542
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    456
    Woebegone
    Nov 19, 2004
  5. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,397
    Howard
    Nov 29, 2004
Loading...

Share This Page