Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'api.': 0.05; 'explicit': 0.07; 'suppose': 0.07; '[0]': 0.09; 'apis': 0.09; 'method,': 0.09; 'method:': 0.09; 'objects,': 0.09; 'raises': 0.09; 'sure,': 0.09; 'system?': 0.09; 'works.': 0.09; 'api': 0.11; 'cc:addr:python- list': 0.11; 'python': 0.11; 'creates': 0.14; '"could': 0.16; '"create': 0.16; '[])': 0.16; 'api,': 0.16; 'constructor.': 0.16; 'file"': 0.16; 'fine.': 0.16; 'hypothetical': 0.16; 'inserting': 0.16; 'libraries.': 0.16; 'objects.': 0.16; 'resource.': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'underlying': 0.16; 'wraps': 0.16; 'write,': 0.16; 'bit': 0.19; 'file,': 0.19; 'not,': 0.20; 'written': 0.21; '>>>': 0.22; 'memory': 0.22; 'issue.': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'creating': 0.23; "aren't": 0.24; 'proxy': 0.24; 'file.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'sort': 0.25; "i've": 0.25; 'permission': 0.26; 'this:': 0.26; 'second': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; "doesn't": 0.30; 'forgot': 0.30; 'moved': 0.30; 'said,': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'usually': 0.31; 'about.': 0.31; 'argue': 0.31; 'object.': 0.31; 'really,': 0.31; 'file': 0.32; 'lists': 0.32; 'probably': 0.32; 'checked': 0.32; 'languages': 0.32; 'another': 0.32; 'open': 0.33; 'everyone': 0.33; 'not.': 0.33; 'sense': 0.34; 'maybe': 0.34; 'could': 0.34; 'agree': 0.35; 'common': 0.35; 'possible.': 0.35; 'something': 0.35; 'case,': 0.35; 'objects': 0.35; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'done,': 0.36; 'object,': 0.36; 'done': 0.36; 'next': 0.36; 'useful': 0.36; 'should': 0.36; 'system,': 0.38; 'server': 0.38; 'initially': 0.38; 'security,': 0.38; 'files': 0.38; 'resource': 0.38; 'rather': 0.38; 'anything': 0.39; 'does': 0.39; 'delete': 0.39; 'either': 0.39; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'most': 0.60; 'numbers': 0.61; 'simple': 0.61; "you're": 0.61; 'first': 0.61; "you've": 0.63; 'making': 0.63; 'real': 0.63; 'such': 0.63; 'more': 0.64; 'talking': 0.65; 'believe': 0.68; 'design.': 0.68; 'special': 0.74; '.....': 0.78; 'potentially': 0.81; 'entity,': 0.84; 'preventing': 0.84; 'rubbish': 0.84; 'subject:skip:o 10': 0.84; 'vital.': 0.84; 'discount': 0.87; 'lists:': 0.91; 'reasons,': 0.91; 'remember,': 0.93; 'story.': 0.93; 'washington': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=EVXCltQ2isHnsNjdXAeGsJMFYicyZveNRQXXiZjXdgQ=; b=pV+LM7iZnbFtIEimycPtGwzlqAFkRlASyEsfoUBhyw3BVPUr35nUlnMDS8YUjEhTd5 LnU9tcALTuVnPPmTXa39RIzQgkt20LXkuiBtpFw6mR6RbW82/3u3KSkEIBJXB8qKMzI0 6q01fVZwAnkiNPn7HqypATdKgw1esWlWxO3lZEz31wiYcEYm18i+2fOvDdPNybxm1nNJ 3GhWSuv+nwGnqj2QsjX0dUZiRuuNIE6PXBdDAjmaNXqGCUkw5hGyy1Vo0eNo7bD5D1ZW OSleAJpZwpIppC1NHPNPw3K0GGOyDYhhtT8aLG43Zf8XcvHd4dtpeZhH2cggC6EAH6Xq 6ZQQ== MIME-Version: 1.0 X-Received: by 10.194.19.104 with SMTP id d8mr28181011wje.24.1368240232777; Fri, 10 May 2013 19:43:52 -0700 (PDT) In-Reply-To: <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Date: Fri, 10 May 2013 19:43:52 -0700 Subject: Re: object.enable() anti-pattern From: Mark Janssen To: "Steven D'Aprano" Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: 99 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368240240 news.xs4all.nl 15959 [2001:888:2000:d::a6]:48203 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45123 >>>>>> There is no sensible use-case for creating a file OBJECT unless it >>>>>> initially wraps an open file pointer. >> >>> So far the only counter-examples given aren't counter-examples ... >> >> Well, sure, if you discount operations like "create this file" and >> queries like "could I delete this file if I wanted to?" [0] as methods >> of the file system rather than of a hypothetical file object. >> >> What about a distributed system? Suppose I want to create a file object >> in one place, and send that object to the another place for the file to >> be read from or written to [1]? Suppose that for security reasons, I >> have to do it that way, because the first place can only create the >> objects, and the second place can only access the underly file contents >> through an existing object? > > Unless you have re-implemented the file I/O system, it doesn't matter. If > your file objects are based on C I/O, then even if the first server > cannot read or write to the files it still creates file objects in an > open state, because that is how C works. > > Or maybe the first server only creates some sort of proxy to the real > underlying file object. Or maybe you're re-implemented the I/O system, > and aren't following C's design. Since you're just making this up as a > thought experiment, anything is possible. > > But either way, that's fine. You've found an object where it does make > sense to have an explicit "make it go" method: first one entity has > permission to construct the object, but not to open the underlying file. > Another entity has permission to open the underlying file, but not to > create the object. I have no idea whether this is a reasonable security > design or not, it actually sounds a bit rubbish to me but what do I know? > So let's treat it as a reasonable design. > > As I've said, repeatedly, that's not what I'm talking about. > > When you DON'T have useful things that can be done with the object before > calling "enable", then it is an anti-pattern to require a separate call > to "enable" method, and the enable functionality should be moved into the > object constructor. If you DO have useful things that can be done, like > pass the object to another entity, for security, then that's a whole > 'nuther story. You're missing one other case: if there's useful things that can be checked before calling enable(). Remember, on multi-user and/or multi-processing systems, there could be contention for a slow resource. If you automatically open a file for write, you're preventing everyone else from writing and potentially reading it. So there is something useful: did that file exist? Is that resource available for writing? Prior to such hi-level languages like Python and reliable hardware, such fine-grained control was important and vital. Now it can probably be relegated to special OS libraries. Mark > Really, what I'm describing is *normal* behaviour for most objects. We > don't usually design APIs like this: > > n = int("42") > n.enable() > m = n + 1 > m.enable() > x = m*2 + n*3 > print x - 1 # oops, raises because I forgot to call x.enable() Again, you only do that for shared resources. In this case, memory would have to be a (protected) shared resources, but the OS manages memory allocation, so it's not an issue. Mark > That's a rubbish API, and for simple data-like objects, we all agree it > is a rubbish API. So why accept the same rubbish API just because the > object is more complicated? I think I just told you, but let me know ..... :) > For my next controversial opinion, I'm going to argue that we should do > arithmetic using numbers rather than by inserting lists inside other > lists: Try arguing that we should have a common message-passing syntax. > # Do this: > > count = 0 > count += 1 > > # Not this: > > count = [] > count.insert(0, []) That's actually what they do in "set theory", believe it or not. MarkJ Tacoma, Washington