[Tutor] [Python-ideas] Access to function objects

Christopher King g.nius.ck at gmail.com
Sat Aug 6 20:24:36 CEST 2011


On Sat, Aug 6, 2011 at 4:10 AM, David Townshend <aquavitae69 at gmail.com>wrote:
>
> def counter(add) as func:
>     if not hasattr(func, 'count'):
>         func.count = 0
>     func.count += 1
>     print(func.count)
>
You already can do that without an as statment.

>>> def counter(add):
    if not hasattr(counter, 'count'):
counter.count = 0
    counter.count += 1
    return counter.count
>>> counter('You ever notice how this parameter is never used anyway?')
Output: 1
>>> counter('Oh well')
Output: 2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110806/c8c6ebef/attachment.html>


More information about the Tutor mailing list