[sclug] Python global variable question

Andrew Nelis andrew.nelis at gmail.com
Thu Nov 26 14:37:54 UTC 2009


2009/11/26 Pieter Claassen <pieter at claassen.co.uk>:
> Hi Andrew,
>
> Sorry, I am daft. Here is a solution I found on the web
>
> self.count=self.static_num().next
>
> def static_num(self):
> ?????? c = 0
> ?????? while True:
> ??????????? c = c + 1
> ??????????? yield c
>
> You get the new increment by calling self.count()

Yes, I hadn't thought of using iterators. The module itertools then
has a counter already:

import itertools

self.count = itertools.count().next

> The state and counter logic is completely stored in the closure so no risk
> of global interference.
>
> When is java going to get first class functions? Things are too easy as they
> are...



More information about the Sclug mailing list