How to read an attribute in another class?
Question: still a beginner in Python, so be kind :) FYI : Python 2.7.5, PySide 1.1.2,
OSX 10.8 Simple question. I have this function :

    
    
    def openFileDialog(self):
        import os
        path, _ = QtGui.QFileDialog.getOpenFileName(self, "Open File", os.getcwd())
        self.label.setText(path)
        print(path)
    

And I want to use this "path" in another function which does not belong to the
same class... I tried different things to display this path in my other
function, for example, I tried

    
    
    print(testWindow.openFileDialog.path)
    

but it didn't work. How would you do this...? Thank you!


Answer: You want to
[return](http://docs.python.org/2/reference/simple_stmts.html#grammar-token-
return_stmt) the path.

    
    
    def openFileDialog(self):
        import os
        path, _ = QtGui.QFileDialog.getOpenFileName(self, "Open File", os.getcwd())
        self.label.setText(path)
        return path
    

This way you can call it as follows

    
    
    path = testWindow.openFileDialog()
    

and store it in a local variable. Then do whatever you want with it.

    
    
    print path
    

If `testWindow` is the class containing `openFileDialog` you will have to
create an instance of the
[class](http://docs.python.org/2/tutorial/classes.html) first, then call the
method on that.

    
    
    x = testWindow()
    path = x.openFileDialog()
    

