Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=python.org; s=200901; t=1368006718; bh=Aw1sszZvaF1vbWdLXaQRJu7ZDtNRBRDj4YmXd9eZUw0=; h=To:From:Subject:Date:Message-ID:References:Mime-Version: Content-Type:Content-Transfer-Encoding:In-Reply-To; b=o+Hdu1x+PwHuoeoJKvhg9uhe8mPParL5jhL+rL9BWQLRMCZ+rNV0tbrLsd6tuGs8x HpFZTjWwBwnK/RE3WUQ8whjdi1N0ViOFy2LebSEyUf/K32ogJCtD8gxOeALed4Sm9D 4NF8Cgse0EqcW5x9EpvYgCEREL4r3pyqLTPAsg4E= X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'context': 0.07; '__init__': 0.09; 'filename': 0.09; 'obj': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'api': 0.11; 'def': 0.12; 'suggest': 0.14; 'creates': 0.14; 'cleanup,': 0.16; 'exc_type,': 0.16; 'filename):': 0.16; 'from:name:christian heimes': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'self.close()': 0.16; 'from:addr:python.org': 0.16; 'creating': 0.23; 'header:User- Agent:1': 0.23; 'fine': 0.24; 'mention': 0.26; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'work.': 0.31; 'too.': 0.31; 'disable': 0.31; 'object.': 0.31; 'steven': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'agree': 0.35; 'but': 0.35; 'object,': 0.36; 'method': 0.36; 'should': 0.36; 'two': 0.37; 'christian': 0.38; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'resource': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'problems.': 0.60; 'kind': 0.63; 'subject:skip:o 10': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Christian Heimes Subject: Re: object.enable() anti-pattern Date: Wed, 08 May 2013 11:51:47 +0200 References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: smtp.semantics.de User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130329 Thunderbird/17.0.5 In-Reply-To: <518a123c$0$11094$c3e8da3@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368006720 news.xs4all.nl 15958 [2001:888:2000:d::a6]:41624 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44946 Am 08.05.2013 10:52, schrieb Steven D'Aprano: > Basically, any time you have two steps required for using an object, e.g. > like this: > > obj = someobject(arg1, arg2) > obj.enable() > > you should move the make-it-work functionality out of the enable method > and into __init__ so that creating the object creates it in a state ready > to work. In general I agree that an object.enable() function is ugly. ;) But it's not necessarily the best solution for all problems. If the resource needs some kind of cleanup, the context api (__enter__() / __exit__()) is perfectly fine way to enable and disable the object. For example: class MyFile: def __init__(self, filename): self.filename = filename def __enter__(self): self.open() return self def __exit__(self, exc_type, exc_val, exc_tb): self.close() I suggest that you mention the context API in your blog post, too. Christian