namespace question

jm.suresh@no.spam.gmail.com jm.suresh at gmail.com
Tue Dec 12 01:57:43 EST 2006


Marc 'BlackJack' Rintsch wrote:
> In <1165904406.053883.129610 at l12g2000cwl.googlegroups.com>,
> jm.suresh at no.spam.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
   <<howToCreateANewNameSpace>>:
       # Objects created here are local to this scope
       a = 1
       b = 2
       global c
       c = a + b

> 
> Ciao,
> 	Marc 'BlackJack' Rintsch




More information about the Python-list mailing list