1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
|
The ``decorator`` module
=============================================================
:author: Michele Simionato
:E-mail: michele.simionato@gmail.com
:version: 2.2.0 (31 July 2007)
:Download page: http://www.phyast.pitt.edu/~micheles/python/decorator-2.2.0.zip
:Installation: ``easy_install decorator``
:License: BSD license
.. contents::
Introduction
------------------------------------------------
Python 2.4 decorators are an interesting example of why syntactic sugar
matters: in principle, their introduction changed nothing, since they do
not provide any new functionality which was not already present in the
language; in practice, their introduction has significantly changed the way
we structure our programs in Python. I believe the change is for the best,
and that decorators are a great idea since:
* decorators help reducing boilerplate code;
* decorators help separation of concerns;
* decorators enhance readability and maintenability;
* decorators are very explicit.
Still, as of now, writing custom decorators correctly requires
some experience and it is not as easy as it could be. For instance,
typical implementations of decorators involve nested functions, and
we all know that flat is better than nested.
The aim of the ``decorator`` module it to simplify the usage of
decorators for the average programmer, and to popularize decorators
usage giving examples of useful decorators, such as ``memoize``,
``tracing``, ``redirecting_stdout``, ``locked``, etc.
The core of this module is a decorator factory called ``decorator``.
All decorators discussed here are built as simple recipes on top
of ``decorator``. You may find their source code in the ``_main.py``
file, which is generated automatically when you run the doctester
(included into the decorator package) on this documentation::
$ python doctester.py documentation.txt
At the same time the doctester runs all the examples contained here as
test cases.
Definitions
------------------------------------
Technically speaking, any Python object which can be called with one argument
can be used as a decorator. However, this definition is somewhat too large
to be really useful. It is more convenient to split the generic class of
decorators in two groups:
+ *signature-preserving* decorators, i.e. callable objects taking a
function as input and returning a function *with the same
signature* as output;
+ *signature-changing* decorators, i.e. decorators that change
the signature of their input function, or decorators returning
non-callable objects.
Signature-changing decorators have their use: for instance the
builtin classes ``staticmethod`` and ``classmethod`` are in this
group, since they take functions and return descriptor objects which
are not functions, nor callables.
However, signature-preserving decorators are more common and easier to
reason about; in particular signature-preserving decorators can be
composed together whereas other
decorators in general cannot (for instance you cannot
meaningfully compose a staticmethod with a classmethod or viceversa).
Writing signature-preserving decorators from scratch is not that
obvious, especially if one wants to define proper decorators that
can accept functions with any signature. A simple example will clarify
the issue.
Statement of the problem
------------------------------
Suppose you want to trace a function: this is a typical use case
for a decorator and you can find in many places code like this:
.. code-block:: python
#<_main.py>
try:
from functools import update_wrapper
except ImportError: # using Python version < 2.5
def decorator_trace(f):
def newf(*args, **kw):
print "calling %s with args %s, %s" % (f.__name__, args, kw)
return f(*args, **kw)
newf.__name__ = f.__name__
newf.__dict__.update(f.__dict__)
newf.__doc__ = f.__doc__
newf.__module__ = f.__module__
return newf
else: # using Python 2.5+
def decorator_trace(f):
def newf(*args, **kw):
print "calling %s with args %s, %s" % (f.__name__, args, kw)
return f(*args, **kw)
return update_wrapper(newf, f)
#</_main.py>
The implementation above works in the sense that the decorator
can accept functions with generic signatures; unfortunately this
implementation does *not* define a signature-preserving decorator, since in
general ``decorator_trace`` returns a function with a
*different signature* from the original function.
Consider for instance the following case:
>>> @decorator_trace
... def f1(x):
... pass
Here the original function takes a single argument named ``x``,
but the decorated function takes any number of arguments and
keyword arguments:
>>> from inspect import getargspec
>>> print getargspec(f1)
([], 'args', 'kw', None)
This means that introspection tools such as pydoc will give
wrong informations about the signature of ``f1``. This is pretty bad:
pydoc will tell you that the function accepts a generic signature
``*args``, ``**kw``, but when you try to call the function with more than an
argument, you will get an error:
>>> f1(0, 1)
Traceback (most recent call last):
...
TypeError: f1() takes exactly 1 argument (2 given)
The solution
-----------------------------------------
The solution is to provide a generic factory of generators, which
hides the complexity of making signature-preserving decorators
from the application programmer. The ``decorator`` factory
allows to define decorators without the need to use nested functions
or classes. As an example, here is how you can define
``decorator_trace``.
First of all, you must import ``decorator``:
>>> from decorator import decorator
Then you must define an helper function with signature ``(f, *args, **kw)``
which calls the original function ``f`` with arguments ``args`` and ``kw``
and implements the tracing capability:
.. code-block:: python
#<_main.py>
def trace(f, *args, **kw):
print "calling %s with args %s, %s" % (f.func_name, args, kw)
return f(*args, **kw)
#</_main.py>
``decorator`` is able to convert the helper function into a
signature-preserving decorator
object, i.e is a callable object that takes a function and returns a
decorated function with the same signature of the original function.
Therefore, you can write the following:
>>> @decorator(trace)
... def f1(x):
... pass
It is immediate to verify that ``f1`` works
>>> f1(0)
calling f1 with args (0,), {}
and it that it has the correct signature:
>>> print getargspec(f1)
(['x'], None, None, None)
The same decorator works with functions of any signature:
>>> @decorator(trace)
... def f(x, y=1, z=2, *args, **kw):
... pass
>>> f(0, 3)
calling f with args (0, 3, 2), {}
>>> print getargspec(f)
(['x', 'y', 'z'], 'args', 'kw', (1, 2))
That includes even functions with exotic signatures like the following:
>>> @decorator(trace)
... def exotic_signature((x, y)=(1,2)): return x+y
>>> print getargspec(exotic_signature)
([['x', 'y']], None, None, ((1, 2),))
>>> exotic_signature()
calling exotic_signature with args ((1, 2),), {}
3
``decorator`` is a decorator
---------------------------------------------
The ``decorator`` factory itself can be considered as a signature-changing
decorator, just as ``classmethod`` and ``staticmethod``.
However, ``classmethod`` and ``staticmethod`` return generic
objects which are not callable, while ``decorator`` returns
signature-preserving decorators, i.e. functions of a single argument.
Therefore, you can write
>>> @decorator
... def tracing(f, *args, **kw):
... print "calling %s with args %s, %s" % (f.func_name, args, kw)
... return f(*args, **kw)
and this idiom is actually redefining ``tracing`` to be a decorator.
We can easily check that the signature has changed:
>>> print getargspec(tracing)
(['func'], None, None, None)
Therefore now ``tracing`` can be used as a decorator and
the following will work:
>>> @tracing
... def func(): pass
>>> func()
calling func with args (), {}
BTW, you may use the decorator on lambda functions too:
>>> tracing(lambda : None)()
calling <lambda> with args (), {}
For the rest of this document, I will discuss examples of useful
decorators built on top of ``decorator``.
``memoize``
---------------------------------------------------------
This decorator implements the ``memoize`` pattern, i.e. it caches
the result of a function in a dictionary, so that the next time
the function is called with the same input parameters the result is retrieved
from the cache and not recomputed.
.. code-block:: python
#<_main.py>
from decorator import *
def getattr_(obj, name, default_thunk):
"Similar to .setdefault in dictionaries."
try:
return getattr(obj, name)
except AttributeError:
default = default_thunk()
setattr(obj, name, default)
return default
@decorator
def memoize(func, *args):
dic = getattr_(func, "memoize_dic", dict)
# memoize_dic is created at the first call
if args in dic:
return dic[args]
else:
result = func(*args)
dic[args] = result
return result
#</_main.py>
Here is a test of usage:
>>> @memoize
... def heavy_computation():
... time.sleep(2)
... return "done"
>>> print heavy_computation() # the first time it will take 2 seconds
done
>>> print heavy_computation() # the second time it will be instantaneous
done
As an exercise, try to implement ``memoize`` *properly* without the
``decorator`` factory.
Notice that this ``memoize`` only works for functions with no keyword
arguments, since it is impossible to memoize correctly something that
depends on mutable arguments. One can relax this requirement, and
allow keyword arguments in the signature: however, if keyword
arguments are passed, the result should not be cached. For an
example see http://www.python.org/moin/PythonDecoratorLibrary
``locked``
---------------------------------------------------------------
There are good use cases for decorators is in multithreaded programming.
For instance, a ``locked`` decorator can remove the boilerplate
for acquiring/releasing locks [#]_.
.. code-block:: python
#<_main.py>
import threading
@decorator
def locked(func, *args, **kw):
lock = getattr_(func, "lock", threading.Lock)
lock.acquire()
try:
result = func(*args, **kw)
finally:
lock.release()
return result
#</_main.py>
.. [#] In Python 2.5, the preferred way to manage locking is via
the ``with`` statement: http://docs.python.org/dev/lib/with-locks.html
To show an example of usage, suppose one wants to write some data to
an external resource which can be accessed by a single user at once
(for instance a printer). Then the access to the writing function must
be locked:
.. code-block:: python
#<_main.py>
import time
datalist = [] # for simplicity the written data are stored into a list.
@locked
def write(data):
"Writing to a sigle-access resource"
time.sleep(1)
datalist.append(data)
#</_main.py>
Since the writing function is locked, we are guaranteed that at any given time
there is at most one writer. An example multithreaded program that invokes
``write`` and prints the datalist is shown in the next section.
``delayed`` and ``threaded``
--------------------------------------------
Often, one wants to define families of decorators, i.e. decorators depending
on one or more parameters.
Here I will consider the example of a one-parameter family of ``delayed``
decorators taking a procedure and converting it into a delayed procedure.
In this case the time delay is the parameter.
A delayed procedure is a procedure that, when called,
is executed in a separate thread after a certain time
delay. The implementation is not difficult:
.. code-block:: python
#<_main.py>
def delayed(nsec):
def call(proc, *args, **kw):
thread = threading.Timer(nsec, proc, args, kw)
thread.start()
return thread
return decorator(call)
#</_main.py>
Notice that without the help of ``decorator``, an additional level of
nesting would have been needed.
Delayed decorators as intended to be used on procedures, i.e.
on functions returning ``None``, since the return value of the original
function is discarded by this implementation. The decorated function returns
the current execution thread, which can be stored and checked later, for
instance to verify that the thread ``.isAlive()``.
Delayed procedures can be useful in many situations. For instance, I have used
this pattern to start a web browser *after* the web server started,
in code such as
>>> @delayed(2)
... def start_browser():
... "code to open an external browser window here"
>>> #start_browser() # will open the browser in 2 seconds
>>> #server.serve_forever() # enter the server mainloop
The particular case in which there is no delay is important enough
to deserve a name:
.. code-block:: python
#<_main.py>
threaded = delayed(0) # no-delay decorator
#</_main.py>
Threaded procedures will be executed in a separated thread as soon
as they are called. Here is an example using the ``write``
routine defined before:
>>> @threaded
... def writedata(data):
... write(data)
Each call to ``writedata`` will create a new writer thread, but there will
be no synchronization problems since ``write`` is locked.
>>> writedata("data1")
<_Timer(Thread-1, started)>
>>> time.sleep(.1) # wait a bit, so we are sure data2 is written after data1
>>> writedata("data2")
<_Timer(Thread-2, started)>
>>> time.sleep(2) # wait for the writers to complete
>>> print datalist
['data1', 'data2']
``blocking``
-------------------------------------------
Sometimes one has to deal with blocking resources, such as ``stdin``, and
sometimes it is best to have back a "busy" message than to block everything.
This behavior can be implemented with a suitable decorator:
.. code-block:: python
#<_main.py>
def blocking(not_avail="Not Available"):
def call(f, *args, **kw):
if not hasattr(f, "thread"): # no thread running
def set_result(): f.result = f(*args, **kw)
f.thread = threading.Thread(None, set_result)
f.thread.start()
return not_avail
elif f.thread.isAlive():
return not_avail
else: # the thread is ended, return the stored result
del f.thread
return f.result
return decorator(call)
#</_main.py>
Functions decorated with ``blocking`` will return a busy message if
the resource is unavailable, and the intended result if the resource is
available. For instance:
>>> @blocking("Please wait ...")
... def read_data():
... time.sleep(3) # simulate a blocking resource
... return "some data"
>>> print read_data() # data is not available yet
Please wait ...
>>> time.sleep(1)
>>> print read_data() # data is not available yet
Please wait ...
>>> time.sleep(1)
>>> print read_data() # data is not available yet
Please wait ...
>>> time.sleep(1.1) # after 3.1 seconds, data is available
>>> print read_data()
some data
``redirecting_stdout``
-------------------------------------------
Decorators help in removing the boilerplate associated to ``try .. finally``
blocks. We saw the case of ``locked``; here is another example:
.. code-block:: python
#<_main.py>
import sys
def redirecting_stdout(new_stdout):
def call(func, *args, **kw):
save_stdout = sys.stdout
sys.stdout = new_stdout
try:
result = func(*args, **kw)
finally:
sys.stdout = save_stdout
return result
return decorator(call)
#</_main.py>
Here is an example of usage:
>>> from StringIO import StringIO
>>> out = StringIO()
>>> @redirecting_stdout(out)
... def helloworld():
... print "hello, world!"
>>> helloworld()
>>> out.getvalue()
'hello, world!\n'
Similar tricks can be used to remove the boilerplate associate with
transactional databases. I think you got the idea, so I will leave
the transactional example as an exercise for the reader. Of course
in Python 2.5 these use cases can also be addressed with the ``with``
statement.
Dealing with third party decorators: ``new_wrapper``
------------------------------------------------------------
Sometimes you find on the net some cool decorator that you would
like to include in your code. However, more often than not the cool
decorator is not signature-preserving. Therefore you may want an easy way to
upgrade third party decorators to signature-preserving decorators without
having to rewrite them in terms of ``decorator``. To this aim the
``decorator`` module provides an utility function called ``new_wrapper``.
``new_wrapper`` takes a wrapper function with a generic signature and returns
a copy of it with the right signature.
For instance, suppose you have a wrapper function ``wrapper`` (or generically
a callable object) with a "permissive" signature (say ``wrapper(*args, **kw)``)
returned by a third party non signature-preserving decorator; let ``model``
be the original function, with a stricter signature; then
``new_wrapper(wrapper, model)``
returns a copy of ``wrapper`` with signature copied from ``model``.
Notice that it is your responsability to make sure that the original
function and the model function have compatibile signature, i.e. that
the signature of the model is stricter (or equivalent) than the signature
of the original function. If not, you will get an error at calling
time, not at decoration time.
With ``new_wrapper`` at your disposal, it is a breeze to define an utility
to upgrade old-style decorators to signature-preserving decorators:
.. code-block:: python
#<_main.py>
def upgrade_dec(dec):
return lambda f : new_wrapper(dec(f), f)
#</_main.py>
``tail_recursive``
------------------------------------------------------------
In order to give an example of usage for ``new_wrapper``, I will show a
pretty slick decorator that converts a tail-recursive function in an iterative
function. I have shamelessly stolen the basic idea from Kay Schluehr's recipe
in the Python Cookbook,
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691.
.. code-block:: python
#<_main.py>
from decorator import new_wrapper
class TailRecursive(object):
"""
tail_recursive decorator based on Kay Schluehr's recipe
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
"""
CONTINUE = object() # sentinel
def __init__(self, func):
self.func = func
self.firstcall = True
def __call__(self, *args, **kwd):
try:
if self.firstcall: # start looping
self.firstcall = False
while True:
result = self.func(*args, **kwd)
if result is self.CONTINUE: # update arguments
args, kwd = self.argskwd
else: # last call
break
else: # return the arguments of the tail call
self.argskwd = args, kwd
return self.CONTINUE
except: # reset and re-raise
self.firstcall = True
raise
else: # reset and exit
self.firstcall = True
return result
tail_recursive = upgrade_dec(TailRecursive)
#</_main.py>
Here the decorator is implemented as a class returning callable
objects. ``upgrade_dec`` converts that class in a factory function
returning functions.
Here is how you apply the upgraded decorator to the good old factorial:
.. code-block:: python
#<_main.py>
@tail_recursive
def factorial(n, acc=1):
"The good old factorial"
if n == 0: return acc
return factorial(n-1, n*acc)
#</_main.py>
>>> print factorial(4)
24
This decorator is pretty impressive, and should give you some food for
your mind ;) Notice that there is no recursion limit now, and you can
easily compute ``factorial(1001)`` or larger without filling the stack
frame. Notice also that the decorator will not work on functions which
are not tail recursive, such as
.. code-block:: python
def fact(n): # this is not tail-recursive
if n == 0: return 1
return n * fact(n-1)
(a function is tail recursive if it either returns a value without
making a recursive call, or returns directly the result of a recursive
call).
Caveats and limitations
-------------------------------------------
The first thing you should be aware of, it the fact that decorators
have a performance penalty.
The worse case is shown by the following example::
$ cat performance.sh
python -m timeit -s "
from decorator import decorator
@decorator
def do_nothing(func, *args, **kw):
return func(*args, **kw)
@do_nothing
def f():
pass
" "f()"
python -m timeit -s "
def f():
pass
" "f()"
On my Linux system, using the ``do_nothing`` decorator instead of the
plain function is more than four times slower::
$ bash performance.sh
1000000 loops, best of 3: 1.68 usec per loop
1000000 loops, best of 3: 0.397 usec per loop
It should be noted that a real life function would probably do
something more useful than ``f`` here, and therefore in real life the
performance penalty could be completely negligible. As always, the
only way to know if there is
a penalty in your specific use case is to measure it.
You should be aware that decorators will make your tracebacks
longer and more difficult to understand. Consider this example:
>>> @tracing
... def f():
... 1/0
Calling ``f()`` will give you a ``ZeroDivisionError``, but since the
function is decorated the traceback will be longer:
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
f()
File "<string>", line 2, in f
File "<stdin>", line 4, in tracing
return f(*args, **kw)
File "<stdin>", line 3, in f
1/0
ZeroDivisionError: integer division or modulo by zero
You see here the inner call to the decorator ``tracing``, which calls
``f(*args, **kw)``, and a reference to ``File "<string>", line 2, in f``.
This latter reference is due to the fact that internally the decorator
module uses ``eval`` to generate the decorated function. Notice that
``eval`` is *not* responsibile for the performance penalty, since is the
called *only once* at function decoration time, and not every time
the decorated function is called.
Using ``eval`` means that ``inspect.getsource`` will not work for decorated
functions. This means that the usual '??' trick in IPython will give you
the (right on the spot) message
``Dynamically generated function. No source code available.``. This
however is preferable to the situation with regular decorators, where
``inspect.getsource`` gives you the wrapper source code which is probably
not what you want:
.. code-block:: python
#<_main.py>
def identity_dec(func):
def wrapper(*args, **kw):
return func(*args, **kw)
return wrapper
@identity_dec
def example(): pass
#</_main.py>
>>> import inspect
>>> print inspect.getsource(example)
def wrapper(*args, **kw):
return func(*args, **kw)
<BLANKLINE>
(see bug report 1764286_ for an explanation of what is happening).
.. _1764286: http://sourceforge.net/tracker/index.php?func=detail&aid=1764286&group_id=5470&atid=105470
At present, there is no clean way to avoid ``eval``. A clean solution
would require to change the CPython implementation of functions and
add an hook to make it possible to change their signature directly.
This will happen in future versions of Python (see PEP 362_) and
then the decorator module will become obsolete.
.. _362: http://www.python.org/dev/peps/pep-0362
For debugging purposes, it may be useful to know that the decorator
module also provides a ``getinfo`` utility function which returns a
dictionary containing information about a function.
For instance, for the factorial function we will get
>>> d = getinfo(factorial)
>>> d['name']
'factorial'
>>> d['argnames']
['n', 'acc']
>>> d['signature']
'n, acc'
>>> d['defaults']
(1,)
>>> d['doc']
'The good old factorial'
In the present implementation, decorators generated by ``decorator``
can only be used on user-defined Python functions or methods, not on generic
callable objects, nor on built-in functions, due to limitations of the
``inspect`` module in the standard library.
Also, there is a restriction on the names of the arguments: if try to
call an argument ``_call_`` or ``_func_`` you will get an AssertionError:
>>> @tracing
... def f(_func_): print f
...
Traceback (most recent call last):
...
AssertionError: You cannot use _call_ or _func_ as argument names!
(the existence of these two reserved names is an implementation detail).
Moreover, the implementation is such that the decorated function contains
a copy of the original function attributes:
>>> def f(): pass # the original function
>>> f.attr1 = "something" # setting an attribute
>>> f.attr2 = "something else" # setting another attribute
>>> traced_f = tracing(f) # the decorated function
>>> traced_f.attr1
'something'
>>> traced_f.attr2 = "something different" # setting attr
>>> f.attr2 # the original attribute did not change
'something else'
That's all folks, enjoy!
LICENCE
---------------------------------------------
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met::
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in bytecode form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
If you use this software and you are happy with it, consider sending me a
note, just to gratify my ego. On the other hand, if you use this software and
you are unhappy with it, send me a patch!
|