[Tutor] data sharing

Kristoffer Erlandsson krier115@student.liu.se
Tue Apr 15 07:23:02 2003


On Tue, Apr 15, 2003 at 05:58:25PM +0700, nano@intermatik.co.id wrote:
> Hi pythonmania,
> 
> Thanks for many advices you've given in my previous problem [list
> comprehension].
> 
> Now, StraightToProblem:
> I have an object "Class_A" that get a big data from MySQL to do
> search/sort etc. How can I share that data, so that if other object
> "Class_B", "Class_C" need the same data, they can use it, without have to
> call it from MySQL.

If I understand you right, you want to give the data to the other
objects, but you just don't want to touch MySQL again, is that right?

So say that we have you first class here

>>> class Class_A:
...     def getBigText(self):
...         self.bigText = getThingsFromMySQL()

If you then add methods like this in your other classes:

>>> class Class_B:
...     def giveText(self, text):
...             self.text = text

and the same for Class_C

You could to something like this in your main program:

a = Class_A()
b = Class_B()
c = Class_C()

a.getBigText()
b.giveText(a.bigText)
c.giveText(a.bigText)

now the objects b and c of Class_A and Class_B respectively will have an
own copy of the text extracted from MySQL. This text they will have in
their attribute text. So we have now that a.bigText == b.text == c.text.
Keep in mind that they now have different copies of the text, so if you
update the text the a object it will not get updated in the other
objects. As I understood it this was what you were after, but of course
I could be mistaken :)

Hope it helps!

Regards,
Kristoffer


> 
> Best Regards,
> 
> Nano'
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Kristoffer Erlandsson
E-mail:  krier115@student.liu.se
ICQ#:    378225