Passing objects around an event queue in Python

So, I have a relatively confusing setup for something I'm working on, explaining the following:

This is python. and a rougher plan, but it covers everything I need. Although the next function of the process is the same, so feel free to clean it if you want.

#timer event that runs every .1 second and processes events in a queue
some_event_timer():
    events.process_next()

class Event_queue:
    def __init__(self):
        self.events = []

    def push(self, event, parameters):
        self.events.insert(len(self.events), event, parameters)

    def process_next(self):
        event = self.pop(0)
        event[0](event[1])

class Foo:            
    def __init__(self, start_value = 1):
        self.value = start_value

    def update_value(self, multiple):
        self.value *= multiple

    def return_bah(self)
        return self.value + 3

class Bar:
    def __init__(self, number1, number2):
        self.init = number1
        self.add = number2

    def print_alt_value(self, in_value):
        print in_value * (self.init + self.add)

These are the barebones of what I have, but this illustrates my problem: Running below

events2 = Event_queue2()
foo1 = Foo(4)   ----> foo1.value = 4 here
bar1 = Bar(4, 2)

events2.push(foo1.update_value,1.5)
events2.push(bar1.print_alt_value,foo1.value)
events2.push(bar.print_alt_value,foo1.return_bah())

events2.process_next() ----> should process update_value to change foo.value to 6
events2.process_next() ----> should process print_alt_value in bar class - expected 36
events2.process_next() ----> should process print_alt_value - expected 54

It was originally expected that my conclusion would be 36 6 * (4 + 2)

I know why it is not there, foo1.value and foo1.return_bah () are passed as an evaluated parameter (correct term?). I really want to pass a reference to a variable or a reference to a method, and not evaluate it when I put it in the event queue.

Can anybody help me.

, , . , , : Python

Python

, .

, , , , foo1.return.bah process_next, , , , event_queue .

:

, , :

, , , .

, . :

names = ["test1", "test2"]
    for name in names:
        names_objs[name] = Foo(4)

for name in names_list:
    events2.push(lambda: names_objs[name].update_value(2))

. , name_objs [name], ​​ name , . , :

name_obj_hold = name_objs[name] 

. name_obj_hold, .

- funcs. , , .

, , - :   names_objs [name].some_func (#something #) , #something # ( , ), .

+4
1

func1 , , func2, func1 , .

d = {"a":1}
def p(val):
  print val

def func1():
  p(d["a"])

def call_it(func):
  func()

call_it(func1)
d["a"] = 111
call_it(func1)

func1, d["a"] , func1 .

:

class EventQueue(object):
  def __init__(self):
    self.events = deque()
  def push(self, callable):
    self.events.append(callable)
  def process_next(self):
    self.events.popleft()()

collections.deque , .

EventQueue, lambdas .

events2 = EventQueue()
foo1 = Foo(4)
bar1 = Bar(4, 2)

events2.push(lambda: foo1.update_value(1.5))
events2.push(lambda: bar1.print_alt_value(foo1.value))
events2.push(lambda: bar1.print_alt_value(foo1.return_bah()))

events2.process_next()
events2.process_next() # 36.0
events2.process_next() # 54.0

:

"" , . partial().

for name in names_list:
  def update(name):
    names_objs[name].update_value(2)
  events2.push(partial(update, name))
+2

All Articles