Re: using "private" parameters as static storage?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jean-Paul Calderone

    Re: using "private" parameters as static storage?

    On Thu, 13 Nov 2008 10:58:49 -0800 (PST), rurpy@yahoo.com wrote:
    >On Nov 13, 11:32 am, "J. Cliff Dyer" <j...@sdf.lones tar.orgwrote:
    >On Thu, 2008-11-13 at 09:38 -0800, Matimus wrote:
    [snip]
    Preserving state is what classes are for.
    >>
    >Preserving state is what *objects* are for.
    >
    >Not exclusively, generators also preserve state.
    >
    >def _spam():
    count = 1
    while 1:
    yield "spam " * count
    count += 1
    >spam = _spam.next()
    >
    Surprise - generators are objects.

    Jean-Paul
  • rurpy@yahoo.com

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

    On Nov 13, 12:05 pm, Jean-Paul Calderone <exar...@divmod .comwrote:
    On Thu, 13 Nov 2008 10:58:49 -0800 (PST), ru...@yahoo.com wrote:
    On Nov 13, 11:32 am, "J. Cliff Dyer" <j...@sdf.lones tar.orgwrote:
    On Thu, 2008-11-13 at 09:38 -0800, Matimus wrote:
    [snip]
    Preserving state is what classes are for.
    >
    Preserving state is what *objects* are for.
    >
    Not exclusively, generators also preserve state.
    >
    def _spam():
    count = 1
    while 1:
    yield "spam " * count
    count += 1
    spam = _spam.next()
    >
    Surprise - generators are objects.
    No surprise. Everything in Python is an object. I was talking at the
    language level, not the implementation level.

    Comment

    • Steven D'Aprano

      #3
      Re: using &quot;private&q uot; parameters as static storage?

      On Thu, 13 Nov 2008 14:05:17 -0500, Jean-Paul Calderone wrote:
      On Thu, 13 Nov 2008 10:58:49 -0800 (PST), rurpy@yahoo.com wrote:
      >>On Nov 13, 11:32 am, "J. Cliff Dyer" <j...@sdf.lones tar.orgwrote:
      >>On Thu, 2008-11-13 at 09:38 -0800, Matimus wrote:
      >[snip]
      >Preserving state is what classes are for.
      >>>
      >>Preserving state is what *objects* are for.
      >>
      >>Not exclusively, generators also preserve state.
      >>
      >>def _spam():
      > count = 1
      > while 1:
      > yield "spam " * count
      > count += 1
      >>spam = _spam.next()
      >>
      >>
      Surprise - generators are objects.
      Yes, technically, but the value of count is not stored as an instance
      attribute: you can't access spam.count. Instead, it is stored in the
      internals of the generator.

      Generators are a special case of closures:

      http://en.wikipedia.org/wiki/Closure_(computer_science)

      Note that in some languages, closures are used to implement objects,
      rather than the other way around.



      --
      Steven

      Comment

      Working...