oop - How to use the data generated in a specific class in another class? (in python) -


i'm new in python , in oo programming, don't know if following question makes sense (...but think it's nice way learn):

i'm trying have 2 classes. 1 generates data (call datagen()) , able process data , give me statistics (call stats()). want put both in different py files keep cleaner , can add methods stats.py without touching datagen.py. this:
in datagen.py

class datagen(object):       def __init__(self, number_of_samples, arg2):         self.x = np.zeros([number_of_samples,1])         # etc..     def samples(self):         # sampling , save in self.x   

in stats.py

class stats(object):       def __init__(self, something):         # here initialize         # etc..     def mean(self):         # calculate mean using datagen.x   

now, , here comes part lost. want stats() work on data belonging instance of datagen(), don't know how link data contained in datagen.x stats, can use data every time sample datagen.samples().

i tried construct instance of dg = datagen(arg1,arg2) , pass object s = stats(dg). however, if initialize way, data used estimate statistics didn't changed after sampled again datagen.samples(). guess every time sample, have create instance s = stats(dg) new data. seems bad... there anyway can attach stats() class data of datagen()? bad idea/horrible construct?

i don't know how should think if construct datagen inherits methods of stats, or similar. if datagen inherits stats, stats needs data datagen, how can solve loop? if stats inherits datagen, need create single instance of stats , sample instead of datagen, stats.datagen.samples() or stats.samples(). wouldn't because rest of code uses datagen() , think better structured if don't use stats() sample!

does above makes sense? comment regarding helpful!

for new python , oo you're getting hang of encapsulation well. want follow observer pattern here 1 object owns info (datagen) , other object interested in info (stats) , wants updated when changes.

the way pass function interested object owner object can called when info changes. function called either callback or listener.

class datagen(object):       def __init__(self, number_of_samples, arg2):         self.x = np.zeros([number_of_samples,1])         self.listeners = list()      def samples(self):         # sampling , save in self.x          # call listener , let know samples changed.         listener in self.listeners:             listener.samples_changed(self.x)      def add_listener(self, listener):         listeners.append(listener)  class stats(object):     def __init__(self, data_gen):         # register 'observer' 'observable'         something.add_listener(self)      def samples_changed(self, samples):         # recalculate mean.      def mean(self):         # calculate mean using datagen.x   

here example of object adding listener object.

  1. stats registers listener datagen
  2. you call samples() on datagen
  3. samples() iterates through of listeners (there may more 1 stats) , calls samples_changed(self.x) on each one, passing new set of samples.
  4. one of listeners (the 1 in case) stats, can update internal state handle new samples.

if @ time want remove stats object must make sure remove datagen.listeners list, otherwise end memory leak.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -