Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can't use class variable with private nested class

Reply
Thread Tools

Can't use class variable with private nested class

 
 
tron.thomas@verizon.net
Guest
Posts: n/a
 
      03-28-2007
The following code will print a message only once:
class PrintOnce:
printOnce = True

def __init__(self):
if PrintOnce.printOnce:
print 'Printing once.'
PrintOnce.printOnce = False

first = PrintOnce()
second = PrintOnce()

The following code will do the same thing for a nested class:
class Outer:
class Inner:
printOnce = True

def __init__(self):
if Outer.Inner.printOnce:
print 'Printing once.'
Outer.Inner.printOnce = False

def __init__(self):
first = Outer.Inner()
second = Outer.Inner()

outer = Outer()


However the following code, which has a private nested class, does not
work:
class Public:
class __Private:
printOnce = True

def __init__(self):
print 'Creating a __Private instance'
if Public.__Private.printOnce:
print 'Printing once.'
Public.__Private.printOnce = False

def __init__(self):
print 'Creating a Public instance'
first = Public.__Private()
second = Public.__Private()

public = Public()

Attempting to run the code will produce this error:
AttributeError: class Public has no attribute '_Private__Private'

What can be done so that this private nested class can have the same
functionality as the public nested class?

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      03-28-2007
<> wrote:
...
> class Outer:
> class Inner:
> printOnce = True
>
> def __init__(self):
> if Outer.Inner.printOnce:
> print 'Printing once.'
> Outer.Inner.printOnce = False
>
> def __init__(self):
> first = Outer.Inner()
> second = Outer.Inner()
>
> outer = Outer()
>
>
> However the following code, which has a private nested class, does not
> work:
> class Public:
> class __Private:
> printOnce = True
>
> def __init__(self):
> print 'Creating a __Private instance'
> if Public.__Private.printOnce:


When, anywhere "immediately inside" a class named X, you use a name
__foo starting with two underscores, that name is mangled to _X__foo.
Here, you're inside class __Private, so the mangling of __Private is to
_Private__Private (I'd actually have expected more stray underscores
hither and thither, but that's the gist of it).

> print 'Printing once.'
> Public.__Private.printOnce = False
>
> def __init__(self):
> print 'Creating a Public instance'
> first = Public.__Private()
> second = Public.__Private()
>
> public = Public()
>
> Attempting to run the code will produce this error:
> AttributeError: class Public has no attribute '_Private__Private'
>
> What can be done so that this private nested class can have the same
> functionality as the public nested class?


Forget all the naming silliness and use self.__class__.printOnce
instead.


Alex
 
Reply With Quote
 
 
 
 
tron.thomas@verizon.net
Guest
Posts: n/a
 
      03-29-2007
On Mar 27, 10:08 pm, a...@mac.com (Alex Martelli) wrote:
>
> Forget all the naming silliness and use self.__class__.printOnce
> instead.
>
> Alex


I tried self.__class__.printOnce and that worked. Thanks for your
help.

 
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
Returning a private nested class of a class template ymost@hotmail.com C++ 3 01-10-2009 03:48 PM
problem with a function template returning a private nested class ofa class template ymost@hotmail.com C++ 2 12-28-2008 10:43 AM
Public Data in Private Class or Private Data in Public Class? DaveLessnau C++ 3 05-16-2005 06:53 PM
RE: Why I use private variables (WAS: RE:"private" variablesa.k.a. name mangling?) Jeremy Bowers Python 3 01-24-2005 10:52 PM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM



Advertisments