2

I'm trying to assign a variable in my inner class with the variable on the outer class.

class OUTER(QtGui.QWidget):
    def __init__(self):        
        super (OUTER, self).__init__()
        self.initUI()
    def number (self):
        self.out = 50
    ...

    class INNER(QtGui.QLCDNumber)
        in = OUTER.out  #error: NameError: name 'OUTER' is not defined

        @pyqtSlot()
        def some_func(self):
            self.display(self.in)

I'm getting an error

NameError: name 'OUTER' is not defined.  

Is there any way to fix this?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140

1 Answers1

2

You can't do this.

OUTER is not defined until the entire outer class declaration is finished. Class bodies are executable code; they are executed at definition time. When the body is defined, it is allocated to the name, but until then the name does not exist.

That is one of the reasons why nesting classes is rarely a good idea in Python. The inner class does not get any special access to the outer class, so there isn't really any reason to nest them at all.

Plus, I've just noticed that you are trying to refer to an instance variable via a class. That can't ever work. OUTER.out does not exist, only instances of OUTER have an out attribute. (What would the value of OUTER.out even be?)

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895