Created
November 11, 2008 00:13
-
-
Save jeremyBanks/23700 to your computer and use it in GitHub Desktop.
([2010-01] this is a bad, bad idea) Re: Comments in http://stackoverflow.com/questions/279561/what-is-the-python-equivalent-of-static-variables-inside-a-function#279592
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
from __future__ import division, with_statement | |
import sys | |
class Foo(object): | |
def bar(self, counter=[0]): | |
counter[0] += 1 | |
print("Counter is %i" % counter[0]) | |
def main(*args): | |
foo = Foo() | |
foo_two = Foo() | |
print("foo.bar():") | |
foo.bar() | |
foo.bar() | |
print("foo_two.bar()") | |
foo_two.bar() | |
foo_two.bar() | |
if __name__ == "__main__": sys.exit(main(*sys.argv[1:])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo.bar(): | |
Counter is 1 | |
Counter is 2 | |
foo_two.bar() | |
Counter is 3 | |
Counter is 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment