Can't use class variable with private nested class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tron.thomas@verizon.net

    Can't use class variable with private nested class

    The following code will print a message only once:
    class PrintOnce:
    printOnce = True

    def __init__(self):
    if PrintOnce.print Once:
    print 'Printing once.'
    PrintOnce.print Once = 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.pri ntOnce:
    print 'Printing once.'
    Outer.Inner.pri ntOnce = 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.__Privat e.printOnce:
    print 'Printing once.'
    Public.__Privat e.printOnce = False

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

    public = Public()

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

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

  • Alex Martelli

    #2
    Re: Can't use class variable with private nested class

    <tron.thomas@ve rizon.netwrote:
    ...
    class Outer:
    class Inner:
    printOnce = True
    >
    def __init__(self):
    if Outer.Inner.pri ntOnce:
    print 'Printing once.'
    Outer.Inner.pri ntOnce = 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.__Privat e.printOnce:
    When, anywhere "immediatel y 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__Priva te (I'd actually have expected more stray underscores
    hither and thither, but that's the gist of it).
    print 'Printing once.'
    Public.__Privat e.printOnce = False
    >
    def __init__(self):
    print 'Creating a Public instance'
    first = Public.__Privat e()
    second = Public.__Privat e()
    >
    public = Public()
    >
    Attempting to run the code will produce this error:
    AttributeError: class Public has no attribute '_Private__Priv ate'
    >
    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

    Comment

    • tron.thomas@verizon.net

      #3
      Re: Can't use class variable with private nested class

      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.

      Comment

      Working...