Re: using "private" parameters as static storage?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Holden

    Re: using "private" parameters as static storage?

    Joe Strout wrote:
    One thing I miss as I move from REALbasic to Python is the ability to
    have static storage within a method -- i.e. storage that is persistent
    between calls, but not visible outside the method. I frequently use
    this for such things as caching, or for keeping track of how many
    objects a factory function has created, and so on.
    >
    This is a pretty bizarre requirement, IMHO. The normal place to keep
    such information is either class variables or instance variables.
    Today it occurred to me to use a mutable object as the default value of
    a parameter. A simple example:
    >
    def spam(_count=[0]):
    _count[0] += 1
    return "spam " * _count[0]
    >
    >>>spam()
    'spam '
    >>>spam()
    'spam spam '
    >
    This appears to work fine, but it feels a little unclean, having stuff
    in the method signature that is only meant for internal use. Naming the
    parameter with an underscore "_count" makes me feel a little better
    about it. But then, adding something to the module namespace just for
    use by one function seems unclean too.
    >
    It's a bad smell.
    What are your opinions on this idiom? Is there another solution people
    generally prefer?
    >
    Ooh, for a change I had another thought BEFORE hitting Send rather than
    after. Here's another trick:
    >
    def spam2():
    if not hasattr(spam2,' count'):spam2.c ount=0
    spam2.count += 1
    return "spam2 " * spam2.count
    >
    This doesn't expose any uncleanliness outside the function at all. The
    drawback is that the name of the function has to appear several times
    within itself, so if I rename the function, I have to remember to change
    those references too. But then, if I renamed a function, I'd have to
    change all the callers anyway. So maybe this is better. What do y'all
    think?
    >
    I think you'd be much better off creating an instance of a class and
    using that.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/

  • Steven D'Aprano

    #2
    Re: using "private&q uot; parameters as static storage?

    On Thu, 13 Nov 2008 13:35:21 -0500, Steve Holden wrote:
    Joe Strout wrote:
    >One thing I miss as I move from REALbasic to Python is the ability to
    >have static storage within a method -- i.e. storage that is persistent
    >between calls, but not visible outside the method. I frequently use
    >this for such things as caching, or for keeping track of how many
    >objects a factory function has created, and so on.
    >>
    This is a pretty bizarre requirement, IMHO. The normal place to keep
    such information is either class variables or instance variables.
    *jaw drops*

    You know, there were one or two programs written before the invention of
    object-oriented programming techniques. Some of them required persistent
    storage between function calls, and global variables have obvious
    disadvantages. That's why the C language uses static variables.


    static-global-variable-1596




    --
    Steven

    Comment

    Working...