13

Basically I'm a C# developer, I know the way C# does, EventHandler, delegate, even...

but whats the best way to implement it on Python.

mjv
  • 73,152
  • 14
  • 113
  • 156
shahjapan
  • 13,637
  • 22
  • 74
  • 104

2 Answers2

21

I think you should be able to use a function:

def do_work_and_notify(on_done):
    // do work
    on_done()

def send_email_on_completion():
    email_send('joe@example.com', 'you are done')

do_work_and_notify(send_email_on_completion)

Functions (and even methods) in python are first-class objects that can be tossed around like anything else in the language.

Emil Ivanov
  • 37,300
  • 12
  • 75
  • 90
2

This question is a lot like Python Observer Pattern: Examples, Tips? which has lots of great answers. There's even an implementation of C#-like events in Python.

Community
  • 1
  • 1
Jason Orendorff
  • 42,793
  • 6
  • 62
  • 96