Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26723 > unrolled thread

Unexpected behavior using contextmanager on a class method

Started byThomas Draper <tgdrape@gmail.com>
First post2012-08-07 08:30 -0700
Last post2012-08-07 17:45 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Unexpected behavior using contextmanager on a class method Thomas Draper <tgdrape@gmail.com> - 2012-08-07 08:30 -0700
    Re: Unexpected behavior using contextmanager on a class method Peter Otten <__peter__@web.de> - 2012-08-07 18:04 +0200
    Re: Unexpected behavior using contextmanager on a class method Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-07 17:45 +0000

#26723 — Unexpected behavior using contextmanager on a class method

FromThomas Draper <tgdrape@gmail.com>
Date2012-08-07 08:30 -0700
SubjectUnexpected behavior using contextmanager on a class method
Message-ID<c3319131-342a-40e9-927e-07448e8ade98@googlegroups.com>
I want to use with..as in a "reversible circuit generator". However, it seems that @contextmanager changes the expected nature of the class. I tried to distill the problem down to a simple example.

import contextlib

class SymList:
    def __init__(self, L=[]):
        self.L = L

    @contextlib.contextmanager
    def SymAdd(self, a):
        self.L.append(a)
        yield
        self.L.append(a)

SL = SymList()
with SL.SymAdd(3):
    SL.L.append(5)
print(SL.L) # Expect and see [3, 5, 3]
SL2 = SymList() # New object. Should have default values.
print(SL2.L) # Expect [] and see [3, 5, 3]

Why is the data member SL2.L referring to the data member SL.L? Has the @contextmanager somehow made all instantions of the class related?


[toc] | [next] | [standalone]


#26726

FromPeter Otten <__peter__@web.de>
Date2012-08-07 18:04 +0200
Message-ID<mailman.3060.1344355474.4697.python-list@python.org>
In reply to#26723
Thomas Draper wrote:

> I want to use with..as in a "reversible circuit generator". However, it
> seems that @contextmanager changes the expected nature of the class. I
> tried to distill the problem down to a simple example.
> 
> import contextlib
> 
> class SymList:

The problem you experience has nothing to do with context managers, you have 
a mutable default argument in your __init__(). 

>     def __init__(self, L=[]):

L is initialised with an empty list exactly once, when the method is 
defined; any changes you make to the list will be seen by all instances that 
use the default. The fix is

      def __init__(self, L=None):
          if L is None:
              L = []

>         self.L = L
> 
>     @contextlib.contextmanager
>     def SymAdd(self, a):
>         self.L.append(a)
>         yield
>         self.L.append(a)
> 
> SL = SymList()
> with SL.SymAdd(3):
>     SL.L.append(5)
> print(SL.L) # Expect and see [3, 5, 3]
> SL2 = SymList() # New object. Should have default values.
> print(SL2.L) # Expect [] and see [3, 5, 3]
> 
> Why is the data member SL2.L referring to the data member SL.L? Has the
> @contextmanager somehow made all instantions of the class related?

[toc] | [prev] | [next] | [standalone]


#26733

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-08-07 17:45 +0000
Message-ID<50215436$0$29978$c3e8da3$5496439d@news.astraweb.com>
In reply to#26723
On Tue, 07 Aug 2012 08:30:15 -0700, Thomas Draper wrote:

> I want to use with..as in a "reversible circuit generator". However, it
> seems that @contextmanager changes the expected nature of the class. I
> tried to distill the problem down to a simple example.

Nothing to do with contextmanager. That's a red-herring. Your error is 
here:

class SymList:
    def __init__(self, L=[]):
        self.L = L

The default value for L is only set *once*, when the function is defined, 
NOT every time the function is called. Later on, in the SymAdd method you 
modify that list in place. So naturally later instances see the changes, 
because you have changed the default list.

You can see this "early binding" of the default value in action with this 
simple example:


import time
def test(x=time.ctime()):  # Default values are set *once*, not each time.
    print(x)

test()
=> always prints Wed Aug  8 03:40:32 2012

(or whatever time the function is defined).

In this example, the default value is a string, and cannot be changed; 
but in your code it is a list, and can be modified in place. Either way, 
the result is the same: you get the same object used as the default, each 
and every time.


In your case, you can fix this problem and get the effect of "late 
binding" like this:

class SymList:
    def __init__(self, L=None):
        if L is None: L = []
        self.L = L


Now each time the method body runs, you get a different empty list.



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web