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


Groups > comp.lang.python > #26726

Re: Unexpected behavior using contextmanager on a class method

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'context': 0.05; 'none:': 0.05; 'default.': 0.07; 'referring': 0.07; 'subject:skip:c 10': 0.07; 'mutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'subject:using': 0.09; 'def': 0.10; 'contextlib': 0.16; 'defined;': 0.16; 'message- id:@dough.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:class': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'example.': 0.17; 'yield': 0.17; 'changes': 0.20; 'import': 0.21; 'object.': 0.22; 'class.': 0.23; 'seems': 0.23; 'tried': 0.25; 'header:User-Agent:1': 0.26; 'skip:@ 10': 0.27; 'header:X-Complaints-To:1': 0.28; 'email name:': 0.29; 'once,': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'expect': 0.31; 'instances': 0.33; 'values.': 0.33; 'problem': 0.33; 'to:addr :python-list': 0.33; 'list': 0.35; 'nature': 0.35; 'expected': 0.35; 'received:org': 0.36; 'method': 0.36; 'should': 0.36; 'why': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'thomas': 0.62; 'circuit': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Unexpected behavior using contextmanager on a class method
Date Tue, 07 Aug 2012 18:04:29 +0200
Organization None
References <c3319131-342a-40e9-927e-07448e8ade98@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p508497e6.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3060.1344355474.4697.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1344355474 news.xs4all.nl 6869 [2001:888:2000:d::a6]:38205
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:26726

Show key headers only | View raw


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?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web