accessing values from different classes in different files

Carlos "Gu?no" Grohmann wrote:

Hello all,

Forgive my ignorance, still very new at OO-programming and wxpython..

I am working on a simple app, which until today was coded all in a
single file, with all the classes and methods inside the main one
'MyFrame'.
Now I decided to separate thing into files to get something easier to
work. So now I have a class onOpenData, which reads a txt file and
returns self.azim, and I have another class PlotPanel, with a function
(method? I always get confused by this nomenclature in python) called
onPlot. While things were in the same file, and all under MyFrame,
things were easy, now that things are separated, I can't access
self.azim:

Traceback (most recent call last):
  File "/home/xxx/PlotPanel.py", line 200, in onPlot
    azim_mod = self.azim
AttributeError: 'PlotPanel' object has no attribute 'azim'

many thanks for your help

Carlos

Lots more detail is needed. But I can still probably help a little. First, a few questions:

1) Do you really mean MyFrame is a class, with nested classes inside it? Or is MyFrame the name of the module?

2) When you were pasting things into other modules, did you paste whole classes, or did you try separately pasting methods? One simplification: Was everything you pasted at outer scope, unindented?:

3) What do you mean the class onOpenData "returns self.azim" Is the class callable? With a name like that, it sounds like it's a method, not a class.

A function is defined outside of a class, and has no self argument. A method is defined inside the class, and by default has self as the first argument, which is how it shares data with other methods on the same class.

I can't think of any reason why a beginner at Python should use nested classes. If that's what you're doing, it's the unnesting that's causing your problems, not the moving to another file. Or more particularly, you were abusing the fact that they were nested to make your code too tightly coupled, and decoupling is causing you pain.

If this is your situation, I'd go back to when it was all together in one file (you do have source-control, or at least regular backups, don't you), and proceed from there in smaller steps. First, remove just one of the nested classes (PlotPanel?), and move it to outer scope. See what happens with just the small change. When you're comfortable with that, then try moving it to another module.

Otherwise, we'd have to see lots more of your code than a traceback.

The kind of structure I might expect, based only on the names you've mentioned is (all off the top of my head):

class MyFrame(wx.Frame):
      def __init__(...
         .....
         self.azim = []
         self.plotpanel = PlotPanel(self, -1, .... , self.azim) .....
       def onOpenData(self, filename): # a method
               ....
              self.azim.append(datafromfile) #I'm guessing this is a list

class PlotPanel(wx.Panel): #the class starts at the left margin; it's not nested
        def __init__(self, parent, ...., azim)
                self.azim = azim #make our own reference to the azim list
                wx.Panel.__init__(self, parent, ....

         def onPlot(self, ....
                ..... azim_mod = self.azim

The key here is that when the PlotPanel was instantiated, it was passed some way of accessing the azim data. If it's a simple list, the way I describe can work, as long as the list is updated in place by anyone
who messes with it. (For example, self.azim.append(), or self.azim[:] = )

A better alternative is usually to use the parent parameter usually passed, and something like
                 azim_mod = self.parent.azim