[Tutor] C-like local static var in Py ? [oops]
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Mon Dec 2 02:05:02 2002
> If we feel uncomfortable about having that '_()' function out there, we
> can still delete it from the namespace, and f() will still continue to
> work:
>
> ###
> >>> del _
> >>> f()
> Call count = 4
> >>> del _
> >>> f()
> Call count = 4
> >>> f()
> Call count = 5
> ###
Urk! Bad cut and paste! It was supposed to look like:
###
>>> del _
>>> f()
Call count = 4
###
I did not mean to paste twice. I'm sorry about that!
I also just realized that '_' is used by the interactive interpreter to
keep track of the very last expression value:
###
>>> "hello" + "world"
'helloworld'
>>> _
'helloworld'
###
And even though the '_' variable has this special meaning only in the
interactive interpreter, it's still really bad form as an example. The
name '_' is potentially confusing, and I should have done something like:
###
def makeCallCounter():
call_count = [0]
def f():
call_count[0] = call_count[0] + 1
print "Call count = %d" % call_count[0]
return f
###
instead. The name 'makeCallCounter' makes it more clear that
makeCallCount() is a function that creates these miniature call_count
functions for us.
Anyway, sorry about those mistakes. I've been reading OCaml code lately;
The people who wrote the OCaml standard library don't appear to like using
long function names. At least, from what I can tell, the code in the
standard library is filled with lovely variable names like 'x', 'a', 'z',
and '_'. I'm afraid that my writing style is slightly... tainted... by
that exposure. *grin*
Good luck to you!