Accessing an instance of a class from within instance of another class

Stefan Krastanov krastanov.stefan at gmail.com
Fri Apr 30 07:05:43 EDT 2010


Hello all,

I have some knowledge of programing in C++ and I believe that I understand
the way one should write programs in Python. But I need some help finding
the Right Way for doing a thing.

Here is the problem:
I have a class (call it Data) that has a number of NumPy arrays and some
methods that get useful information from the arrays (math stuff).
I have two other classes (called Viewer1 and Viewer2) (they are subclasses
of QAbstractTableModel but that's not important).
I am working in my code with one instance of each class. Viewer1 and Viewer2
must be able to call methods from the Data instance, but as the instance of
Data is constantly updated, I cannot just copy it.
I have thought of the following solution:

data = Data()

class Viewer(***):
    def __init__(self, ***, data):
        ***
        self.D = [data, ]

    def Data(self):
        return self.D[0]

    def SomeOtherFunction(self):
         self.Data.something()

It's an ugly way to implement a pointer or something like it. What is the
right way?

Cheers
Stefan Krastanov

P.S. Maybe it's bad idea to use two different view classes, but that is
another question.
P.P.S. Maybe I'm breaking the encapsulation. What should I do?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100430/2a50e8b8/attachment.html>


More information about the Python-list mailing list