RE: static keyword

Discussion in 'Python' started by Robert Brewer, Apr 29, 2004.

  1. Nick Jacobson wrote:
    > I believe the following "static" command would be useful in Python.
    >
    > def foo():
    > static i = [10, 11]
    > static firstcall = True
    > if firstcall:
    > print "First pass"
    > firstcall = False
    > i[0] += 1
    > print i[0]
    > foo()
    > foo()
    >
    >
    > This would output:
    >
    > First pass
    > 11
    > 12


    Bah. All these old fogies with their default arg hacks, when Nick
    *clearly* wants a generator:

    >>> def foo():

    .... i = 10
    .... print "First pass"
    .... while True:
    .... i += 1
    .... yield i
    ....
    >>> g = foo()
    >>> g.next()

    First pass
    11
    >>> g.next()

    12


    Robert Brewer
    MIS
    Amor Ministries
    Robert Brewer, Apr 29, 2004
    #1
    1. Advertising

  2. Robert Brewer

    Rick Ratzel Guest

    Robert beat me to it...I was going to reply with a generator
    example! Indeed...Nick *clearly* wants a generator. By far the most
    elegant IMO.

    -Rick

    Robert Brewer wrote:
    > Nick Jacobson wrote:
    >
    >>I believe the following "static" command would be useful in Python.
    >>
    >>def foo():
    >> static i = [10, 11]
    >> static firstcall = True
    >> if firstcall:
    >> print "First pass"
    >> firstcall = False
    >> i[0] += 1
    >> print i[0]
    >>foo()
    >>foo()
    >>
    >>
    >>This would output:
    >>
    >>First pass
    >>11
    >>12

    >
    >
    > Bah. All these old fogies with their default arg hacks, when Nick
    > *clearly* wants a generator:
    >
    >
    >>>>def foo():

    >
    > ... i = 10
    > ... print "First pass"
    > ... while True:
    > ... i += 1
    > ... yield i
    > ...
    >
    >>>>g = foo()
    >>>>g.next()

    >
    > First pass
    > 11
    >
    >>>>g.next()

    >
    > 12
    >
    >
    > Robert Brewer
    > MIS
    > Amor Ministries
    >
    >
    Rick Ratzel, Apr 29, 2004
    #2
    1. Advertising

  3. Robert Brewer

    Terry Reedy Guest

    "Rick Ratzel" <> wrote in message
    news:40912de6$0$46516$...
    > Robert beat me to it...I was going to reply with a generator
    > example! Indeed...Nick *clearly* wants a generator. By far the most
    > elegant IMO.
    > Robert Brewer wrote:
    > > Nick Jacobson wrote:
    > > Bah. All these old fogies with their default arg hacks, when Nick
    > > *clearly* wants a generator:


    > >>>>def foo():

    > > ... i = 10
    > > ... print "First pass"
    > > ... while True:
    > > ... i += 1
    > > ... yield i
    > >
    > >>>>g = foo()
    > >>>>g.next()

    > >
    > > First pass
    > > 11


    Unless, of course, Nick's foo is a simplified illustrative example and he
    wants 'static' effect in functions with parameters.

    TJR
    Terry Reedy, Apr 29, 2004
    #3
  4. Yes, that is excellent. Thank you very much. :)

    The good news: you can reset all your "static" data whenever you want,
    by calling foo(). Can't do that in C.

    The bad news: I just hope I don't forget and call foo() instead of
    g.next().
    I would rather the command foo() by default call the next iteration,
    and, say, foo().reset() would recall the function from scratch. But
    that's neither here nor there..

    "Robert Brewer" <> wrote in message news:<>...
    > Nick Jacobson wrote:
    > > I believe the following "static" command would be useful in Python.
    > >
    > > def foo():
    > > static i = [10, 11]
    > > static firstcall = True
    > > if firstcall:
    > > print "First pass"
    > > firstcall = False
    > > i[0] += 1
    > > print i[0]
    > > foo()
    > > foo()
    > >
    > >
    > > This would output:
    > >
    > > First pass
    > > 11
    > > 12

    >
    > Bah. All these old fogies with their default arg hacks, when Nick
    > *clearly* wants a generator:
    >
    > >>> def foo():

    > ... i = 10
    > ... print "First pass"
    > ... while True:
    > ... i += 1
    > ... yield i
    > ...
    > >>> g = foo()
    > >>> g.next()

    > First pass
    > 11
    > >>> g.next()

    > 12
    >
    >
    > Robert Brewer
    > MIS
    > Amor Ministries
    >
    Nick Jacobson, Apr 29, 2004
    #4
  5. Robert Brewer

    Peter Hansen Guest

    Nick Jacobson wrote:

    > Yes, that is excellent. Thank you very much. :)
    >
    > The good news: you can reset all your "static" data whenever you want,
    > by calling foo(). Can't do that in C.
    >
    > The bad news: I just hope I don't forget and call foo() instead of
    > g.next().
    > I would rather the command foo() by default call the next iteration,
    > and, say, foo().reset() would recall the function from scratch. But
    > that's neither here nor there..


    Do this then (and this is, if not a hack, at least unusual style):

    >>>def foo_reset():

    # same as previous foo was, just renamed

    >>>foo = foo_reset().next
    >>>foo()

    First pass
    11
    >>>foo()

    12

    Happy now? ;-)

    -Peter
    Peter Hansen, Apr 30, 2004
    #5
  6. Robert Brewer

    Ekki Guest

    (Nick Jacobson) wrote in message news:<>...
    > The bad news: I just hope I don't forget and call foo() instead of
    > g.next().
    > I would rather the command foo() by default call the next iteration,
    > and, say, foo().reset() would recall the function from scratch. But
    > that's neither here nor there..


    C++ functors (analogous to Python callables) do what you want and a
    lot more. I am sure your problem does not need to get to this level,
    but here is some food for thought. Notice the absence of
    if-statements.

    class F:
    def __call__(self):
    print "First pass"
    self.i = [10, 11] # initialization
    self.first_call = self.__call__ # metaprogramming fun
    self.__call__ = self.call # metaprogramming fun
    self()
    def call(self):
    self.i[0] += 1
    print self.i[0]
    def reset(self):
    self.__call__ = self.first_call # metaprogramming fun
    f = F()

    f()
    f()

    f.reset()
    f()
    f()

    The full power of functors/callables only shows up when you combine
    them with inheritance.

    regards,

    Hung Jung
    Ekki, Apr 30, 2004
    #6
  7. Peter Hansen <> wrote in message news:<>...
    > Nick Jacobson wrote:
    >
    > > Yes, that is excellent. Thank you very much. :)
    > >
    > > The good news: you can reset all your "static" data whenever you want,
    > > by calling foo(). Can't do that in C.
    > >
    > > The bad news: I just hope I don't forget and call foo() instead of
    > > g.next().
    > > I would rather the command foo() by default call the next iteration,
    > > and, say, foo().reset() would recall the function from scratch. But
    > > that's neither here nor there..

    >
    > Do this then (and this is, if not a hack, at least unusual style):
    >
    > >>>def foo_reset():

    > # same as previous foo was, just renamed
    >
    > >>>foo = foo_reset().next
    > >>>foo()

    > First pass
    > 11
    > >>>foo()

    > 12
    >
    > Happy now? ;-)
    >
    > -Peter


    Yes, in fact that's very clever. Thanks! :)
    Nick Jacobson, Apr 30, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2ZQ==?=

    Static keyword usage in asp.net?

    =?Utf-8?B?RGF2ZQ==?=, Nov 18, 2005, in forum: ASP .Net
    Replies:
    11
    Views:
    5,461
    =?Utf-8?B?RGF2ZQ==?=
    Nov 29, 2005
  2. Matt
    Replies:
    3
    Views:
    1,101
    aatishp
    Mar 15, 2012
  3. cppaddict
    Replies:
    2
    Views:
    349
    cppaddict
    May 2, 2004
  4. Replies:
    6
    Views:
    435
    Peter Otten
    May 10, 2007
  5. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    337
Loading...

Share This Page