<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Using zope.event — zope.event 4.3.0 documentation</title> <link rel="stylesheet" href="_static/classic.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <script type="text/javascript" src="_static/language_data.js"></script> <link rel="index" title="Index" href="genindex.html" /> <link rel="search" title="Search" href="search.html" /> <link rel="next" title="Theory of Operation" href="theory.html" /> <link rel="prev" title="zope.event Documentation" href="index.html" /> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="theory.html" title="Theory of Operation" accesskey="N">next</a> |</li> <li class="right" > <a href="index.html" title="zope.event Documentation" accesskey="P">previous</a> |</li> <li class="nav-item nav-item-0"><a href="index.html">zope.event 4.3.0 documentation</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="module-zope.event"> <span id="using-zope-event"></span><span id="usage-docs"></span><h1>Using <a class="reference internal" href="#module-zope.event" title="zope.event"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zope.event</span></code></a><a class="headerlink" href="#module-zope.event" title="Permalink to this headline">¶</a></h1> <p>At its core, <a class="reference internal" href="#module-zope.event" title="zope.event"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zope.event</span></code></a> simply consists of two things: a list of subscribers (callable objects), and an API function (<a class="reference internal" href="api.html#zope.event.notify" title="zope.event.notify"><code class="xref py py-func docutils literal notranslate"><span class="pre">notify()</span></code></a>) that invokes those subscribers in order.</p> <div class="section" id="notifications"> <h2>Notifications<a class="headerlink" href="#notifications" title="Permalink to this headline">¶</a></h2> <p>Alerting subscribers that an event has occurred is referred to as “notifying them”, or sometimes “sending them the event.”</p> <p>The package provides a <a class="reference internal" href="api.html#zope.event.notify" title="zope.event.notify"><code class="xref py py-func docutils literal notranslate"><span class="pre">notify()</span></code></a> function, which is used to notify subscribers that something has happened:</p> <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">MyEvent</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="gp">... </span> <span class="k">pass</span> <span class="gp">>>> </span><span class="n">event</span> <span class="o">=</span> <span class="n">MyEvent</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="n">event</span><span class="p">)</span> </pre></div> </div> <p>The notify function is called with a single object, which we call an event. Any object will do:</p> <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> </pre></div> </div> <p>Our subscriber list is currently empty, so nothing happened in response to these notifications.</p> </div> <div class="section" id="subscribers"> <h2>Subscribers<a class="headerlink" href="#subscribers" title="Permalink to this headline">¶</a></h2> <p>A <em>subscriber</em> is a callable object that takes one argument, an object that we call the <em>event</em>.</p> <p>Application code can manage subscriptions by manipulating the list that <code class="docutils literal notranslate"><span class="pre">zope.event</span></code> maintains. This list starts out empty.</p> <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">zope.event</span> <span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">subscribers</span> <span class="go">[]</span> </pre></div> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>Users of higher-level event frameworks will not typically need to modify <em>this</em> subscriber list directly. Generally, such event (or application) frameworks will provide more sophisticated subscription mechanisms that build on this simple mechanism. The frameworks will install subscribers that then distribute the event to other subscribers based on event types or data.</p> <p>A simple framework that is based on the class hierarchy is distributed with this package and described in <a class="reference internal" href="classhandler.html"><span class="doc">Class-based event handlers</span></a>.</p> <p class="last">A higher-level event framework is distributed with <code class="xref py py-mod docutils literal notranslate"><span class="pre">zope.component</span></code>. For information on using <a class="reference internal" href="#module-zope.event" title="zope.event"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zope.event</span></code></a> together with <code class="xref py py-mod docutils literal notranslate"><span class="pre">zope.component</span></code>, see <a class="reference external" href="http://zopecomponent.readthedocs.io/en/latest/event.html">zope.component’s documentation</a>.</p> </div> <div class="section" id="trivial-subscribers"> <h3>Trivial Subscribers<a class="headerlink" href="#trivial-subscribers" title="Permalink to this headline">¶</a></h3> <p>As mentioned above, subscribers are simply callable objects that are added to the subscriptions list:</p> <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">event</span><span class="p">):</span> <span class="gp">... </span> <span class="k">print</span> <span class="s1">'got:'</span><span class="p">,</span> <span class="n">event</span> <span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">subscribers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="go">got: 42</span> <span class="gp">>>> </span><span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="n">event</span><span class="p">):</span> <span class="gp">... </span> <span class="k">print</span> <span class="s1">'also got:'</span><span class="p">,</span> <span class="n">event</span> <span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">subscribers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">g</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="go">got: 42</span> <span class="go">also got: 42</span> </pre></div> </div> <p>To unsubscribe, simply remove a subscriber from the list:</p> <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">subscribers</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="go">also got: 42</span> <span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">subscribers</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">g</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> </pre></div> </div> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="index.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Using <code class="docutils literal notranslate"><span class="pre">zope.event</span></code></a><ul> <li><a class="reference internal" href="#notifications">Notifications</a></li> <li><a class="reference internal" href="#subscribers">Subscribers</a><ul> <li><a class="reference internal" href="#trivial-subscribers">Trivial Subscribers</a></li> </ul> </li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="index.html" title="previous chapter"><code class="docutils literal notranslate"><span class="pre">zope.event</span></code> Documentation</a></p> <h4>Next topic</h4> <p class="topless"><a href="theory.html" title="next chapter">Theory of Operation</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/usage.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="theory.html" title="Theory of Operation" >next</a> |</li> <li class="right" > <a href="index.html" title="zope.event Documentation" >previous</a> |</li> <li class="nav-item nav-item-0"><a href="index.html">zope.event 4.3.0 documentation</a> »</li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright 2010, Zope Foundation and Contributors. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.3. </div> </body> </html>