[Subversion] / simplegeneric / README.txt  

Diff of /simplegeneric/README.txt

Parent Directory | Revision Log

version 2244, Fri Oct 6 01:03:10 2006 UTC version 2245, Wed Oct 11 16:59:38 2006 UTC
Line 2 
Line 2 
 Trivial Generic Functions  Trivial Generic Functions
 =========================  =========================
   
   (NEW in 0.6: `Inspection and Extension`_, and thread-safe method registration.)
   
 The ``simplegeneric`` module lets you define simple single-dispatch  The ``simplegeneric`` module lets you define simple single-dispatch
 generic functions, akin to Python's built-in generic functions like  generic functions, akin to Python's built-in generic functions like
 ``len()``, ``iter()`` and so on.  However, instead of using  ``len()``, ``iter()`` and so on.  However, instead of using
Line 145 
Line 147 
     ...      ...
   
   
   Inspection and Extension
   ------------------------
   
   You can find out if a generic function has a method for a type or object using
   the ``has_object()`` and ``has_type()`` methods::
   
       >>> move.has_object(zig)
       True
       >>> move.has_object(42)
       False
   
       >>> move.has_type(X)
       True
       >>> move.has_type(float)
       False
   
   Note that ``has_type()`` only queries whether there is a method registered for
   the *exact* type, not subtypes or supertypes::
   
       >>> class Z(X): pass
       >>> move.has_type(Z)
       False
   
   You can create a generic function that "inherits" from an existing generic
   function by calling ``generic()`` on the existing function::
   
       >>> move2 = generic(move)
       >>> move(2101, "war")
       In AD 2101, war was beginning.
   
   Any methods added to the new generic function override *all* methods in the
   "base" function::
   
       >>> @move2.when_type(X)
       ... def move2_X(item, target):
       ...     print "You have no chance to survive make your %s!" % (target,)
   
       >>> move2(X(), "time")
       You have no chance to survive make your time!
   
       >>> move2(Y(), "time")
       You have no chance to survive make your time!
   
   Notice that even though ``move()`` has a method for type ``Y``, the method
   defined for ``X`` in ``move2()`` takes precedence.  This is because the
   ``move`` function is used as the ``default`` method of ``move2``, and ``move2``
   has no method for type ``Y``::
   
       >>> move2.default is move
       True
       >>> move.has_type(Y)
       True
       >>> move2.has_type(Y)
       False
   
   
 Limitations  Limitations
 -----------  -----------
   


Generate output suitable for use with a patch program
Legend:
Removed from v.2244  
changed lines
  Added in v.2245

cvs-admin@eby-sarna.com

Powered by ViewCVS 1.0-dev

ViewCVS and CVS Help