Path: csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.027 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'test,': 0.04; '>>>': 0.11; 'def': 0.14; 'definition.': 0.16; 'f()': 0.16; 'to:name:python- list (general)': 0.16; 'object': 0.22; 'received:209.85.214.174': 0.23; 'received:mail-iw0-f174.google.com': 0.23; 'header:In-Reply- To:1': 0.23; 'second': 0.27; 'received:209.85.214': 0.28; 'class': 0.29; 'message-id:@mail.gmail.com': 0.29; 'subject:?': 0.29; 'regardless': 0.29; "skip:' 10": 0.30; 'to:addr:python-list': 0.31; 'none': 0.32; 'whatever': 0.34; 'pass': 0.34; 'skip:_ 10': 0.36; 'subject:can': 0.36; 'case,': 0.37; 'received:209.85': 0.38; 'received:google.com': 0.38; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; '"perhaps': 0.84; 'subject:tell': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=FzNS8aoLnsFZ2KogX8ajzO6D3amrbYPgLjMeUWeBZrk=; b=rTZ+Gsy++aNnAkpSnzFMpUnk1ksymHcCI3Ae+4b6PXS/EY3u90yVeoCBtxHOMxp+pk FSIvZaTMw6gQR6LtghBUNDwAv7NS4+OEV1QM/tta41pBe2TEj694KSNQTxgwW21Yhpbm x1z81CVFMadRjGsnlO8PXMR3FuYLfBPCS7rQg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=cC7kY0QNpZ+GofevHd0lLfKcCJjnyPF8FIaw4wzj4Q+qO4He/hpKlhaEms09xn7NwC GSGF+eQBhFlJynO8QPmSAtvax1OSTJ5E9FlhvcQ+pv5XEfHu0M3tzrR8vk0eeGNZOcLH P7Z8vtYFeEhmqpayorpXt8X/ZZn2nEPBhYIyM= MIME-Version: 1.0 In-Reply-To: References: Date: Tue, 1 Feb 2011 13:38:34 -0500 Subject: Re: How can I tell if I am inside a context manager? From: Gerald Britton To: "python-list (General)" Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 30 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1296585517 news.xs4all.nl 81476 [::ffff:82.94.164.166]:34888 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55719 "Perhaps something like this:" >>x = open('somefile') >>if hasattr(x, '__enter__'): >> return false >>with open('somefile') as x: >> do_something() >>> class f(): def __init__(s): pass def __enter__(s): return s def __exit__(s,a,b,c): return None >>> x = f() >>> hasattr(x, '__enter__') True >>> with f() as x: hasattr(x,'__enter__') True >>> As you can see, the object has a '__enter__' method regardless of how it was created. Whatever the test, it needs to return False in the first case and True in the second case, without modifying the class definition. Gerald Britton