namespace question

J

jm.suresh

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.
 
M

Marc 'BlackJack' Rintsch

In <[email protected]>,
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
 
J

jm.suresh

Marc said:
In <[email protected]>,


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
 
F

Fredrik Lundh

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>
 
P

Piet van Oostrum

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
 
P

Paul Boddie

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top