Can't use class variable with private nested class

Discussion in 'Python' started by tron.thomas@verizon.net, Mar 28, 2007.

  1. Guest

    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?
    , Mar 28, 2007
    #1
    1. Advertising

  2. <> 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
    Alex Martelli, Mar 28, 2007
    #2
    1. Advertising

  3. Guest

    On Mar 27, 10:08 pm, (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.
    , Mar 29, 2007
    #3
    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. CoolPint
    Replies:
    8
    Views:
    972
    Jeff Schwab
    Dec 14, 2003
  2. DaveLessnau
    Replies:
    3
    Views:
    424
    Howard
    May 16, 2005
  3. Jeremy Bowers
    Replies:
    3
    Views:
    510
    Steven Bethard
    Jan 24, 2005
  4. Replies:
    2
    Views:
    540
  5. Replies:
    3
    Views:
    578
    Bertrand
    Jan 10, 2009
Loading...

Share This Page