Lateral association between an object and a function assigned to variable
I have objects defined that are evolving under a subroutine, and under subsubroutines. And I want to probe the status of these variables via probes.
For example, lets call this class drunk.
class drunk (object):
def __init__(self):
self开发者_如何学Python.position = 0
self.probability = 0.5
def take_a_step():
# code that will make the drunkard change its position
Then, I may generate many drunkards and put them into a class crowd.
class crowd (object):
def __init__(self):
self.inidividuals = num.array([])
I would like to create a "probe" object that can be assigned into any of those objects, but I have implementation problems.
class probe (object):
def __init__(self):
self.function = None
self.data = num.array([])
I want to assign probes to any drunkard, a probe will extract any information from a crowd, or a drunk, that is defined in a function residing in probe.function.
But as far as I am concerned, I can assign probes by putting them into a list within these classes, and call them by relating to the functions within the methods of the classes crowd and drunk.
But in order to do this,
probe_mean = probe()
def probe_function():
#
#
#
probe_mean.function = probe_function()
But since I want to refer to a variable within a class that the function will be within the list of, It seems that I must define the function as
But in order to do this,
probe_mean = probe()
def probe_function(probed):
return probed.position.mean()
probe_mean.function = probe_function(drunk_53)
drunk_53.probes.append(probe_mean)
I have a feeling from the fact that these to objects should refer to each other in order for this to work, this is not he way it should be.
I thought that there must be a way to deal with this without re-introducing drunk_53 to the function, something like a super(), that will enable a vertical association.
I'm not experienced in OOP, so I'm asking for the most suitable way (Pythonic, elegant etc.) to do this. What are your opinions? What approach would you take?
精彩评论