using "private" parameters as static storage?

Aaron Brady castironpi at gmail.com
Fri Nov 14 03:33:27 EST 2008


On Nov 13, 10:25 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Fri, 14 Nov 2008 13:35:02 +1100, Ben Finney wrote:
> > Instead, it looks like you're falling foul of one of the classic
> > mistakes in the “How to ask questions the smart way” document: you've
> > got a goal, but you're assuming that you need to use a specific tool to
> > get there. Instead, you're being shown that your assumption is false:
> > there's a better tool available to achieve your goal.
>
> For the sake of the argument I will accept your characterization of Joe's
> question. Even given that, you have still made an error: Joe hasn't been
> shown that his assumption is wrong, he's merely had a bunch of people
> declare, without evidence or even reason, that he is wrong. If anyone has
> given any reason for avoiding the idiom that doesn't boil down to "I
> don't like it!" (I need a smiley for pursed lips), I haven't read it.
>
> I for one do not think he is wrong, and I have given reasons for
> preferring the idiom:
>
> def parrot(public_args, _cache={})  # or similar
>
> for implementing caches and similar internal storage. It's quick, it's
> easy, it's lightweight, and it exposes the cache to those who need it,
> e.g. for testing.

Does it follow that variables should be exposed too to those who need
it, e.g. for testing?

> If anyone has a *reason* why this idiom is harmful, please say so.

Or forever hold his peace?

I've alluded to them before.  The reasons are *NOT STRONG*.  They are
mild, and do not (*NOT*) constitute a conclusive case.  (Not.)  The
idiom is not harmful, but here is what whatever case there is against
it is.

1.  It's an argument list, not an argument-and-static-variable list.
2.  Mistakes by the caller.
3.  BIG gotcha.
4.  Not obvious from the syntax.  Much of Python is; this isn't, it's
arbitrarily assigned.
5.  Arnaud's 'static' decorator nearly as convenient and much clearer.
6.  Signature can get really long with a few arguments and a few
statics.  Split into two statements, one being a decorator.

Even combined, these don't warrant the effort to remove the feature.

P.S.  At least it's not globals!  Ha ha ha.



More information about the Python-list mailing list