Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > namespace question

Reply
Thread Tools

namespace question

 
 
jm.suresh@no.spam.gmail.com
Guest
Posts: n/a
 
      12-12-2006
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

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      12-12-2006
In < .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
 
Reply With Quote
 
 
 
 
jm.suresh@no.spam.gmail.com
Guest
Posts: n/a
 
      12-12-2006

Marc 'BlackJack' Rintsch wrote:
> In < .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


 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-12-2006
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>

 
Reply With Quote
 
Piet van Oostrum
Guest
Posts: n/a
 
      12-13-2006
>>>>> "" <> (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:
 
Reply With Quote
 
Paul Boddie
Guest
Posts: n/a
 
      12-13-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR CS0234: The type or namespace name 'DataAccessHelper' does not exist in the namespace 'BCC' (are you missing an assembly reference?) li.eddie@gmail.com ASP .Net 0 01-06-2006 11:31 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Reaching into the default namespace when using another namespace. Jason Heyes C++ 1 11-19-2004 02:36 AM
Namespace: Is it a scope or a namespace? Anonymous C++ 3 08-18-2003 01:31 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. Èý¹â ASP .Net 1 07-29-2003 04:31 PM



Advertisments