Generator metadata/attributes

Rob Williscroft rtw at freenet.co.uk
Thu Jan 8 15:42:55 CET 2009


 wrote in news:053df793-9e8e-4855-aba1-f92482cd8922
@v31g2000vbb.googlegroups.com in comp.lang.python:

> class TaggedWrapper():
> 
>     def __init__(self, generator, logMixin, stream):
>         self.__generator = generator
>         self.__tag = '%s@%s' % (logMixin.describe(), stream)
>         logMixin._debug('Created %s' % self)

Note that "self" in the above is the instance of the wrapper class 
TaggedWrapper, not the class that is having its (generator) method
decorated.

import logging
logging.basicConfig( level = logging.DEBUG )

def mydecorator( f ):
  def decorated(self, *args):
    logging.debug( "Created %s", self.__class__.__name__ )
    for i in f(self, *args):
      yield i
  return decorated
  
class Example( object ):
  @mydecorator
  def foo(self, a, b, ):
    yield 1 + a + b
    
e = Example()
for i in e.foo( 2, 3 ):
  print( i )


Output of the above is:

    	DEBUG:root:Created Example
    	6

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list