Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2a.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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'argument': 0.05; 'one?': 0.05; 'explicit': 0.07; 'function,': 0.09; 'method,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'random': 0.14; '(either': 0.16; 'callable': 0.16; 'closure,': 0.16; 'fiddle': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterates': 0.16; "object's": 0.16; 'pointer,': 0.16; 'subject: \n ': 0.16; 'subject:More': 0.16; 'wrote:': 0.18; 'any,': 0.19; 'thu,': 0.19; 'feb': 0.22; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'question': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'function': 0.29; '[1]': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'usually': 0.31; '13,': 0.31; 'assert': 0.31; "d'aprano": 0.31; 'implicit': 0.31; 'really,': 0.31; 'steven': 0.31; 'subject:other': 0.31; 'probably': 0.32; 'critical': 0.32; 'actual': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'data,': 0.36; 'maintained': 0.36; 'object,': 0.36; 'doing': 0.36; 'method': 0.36; 'similar': 0.36; 'two': 0.37; 'skip:[ 10': 0.38; 'pm,': 0.38; 'rather': 0.38; 'anything': 0.39; 'ensure': 0.60; 'name:': 0.61; 'course': 0.61; "you're": 0.61; 'making': 0.63; 'kind': 0.63; 'different': 0.65; 'difficulty': 0.68; 'default': 0.69; 'confirming': 0.84; 'generation,': 0.84; 'proves': 0.84; 'subject:!)': 0.84; 'subject:via': 0.84; 'to:none': 0.92; 'state.': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Xr39bZQHhpQaFgkFS+XLvE2lL6WMJRu//2u0DUsxd9E=; b=m4kpl2m+9tMm1auh4/jd1K0HPljaSGzAxF8ojkpwU/BNh68lOrp5lJmgwBa6aP3lM6 GiO+UP+x0tIXcyuvBSOohGy2FVBKKg3G0tt5pPkq0gqZkxAbj73RoXBAtAPBAkjsgJsT kKkpgt6G6va3qmpsNnZnJY12yyttVpOXVktu/D3CsyIG7RjUhAqEJGmdHjdp5NgNu8L4 pBFHBcy8RNt3ecFvS2EroD87aurpB+W+gC5ncWyzWql7CZ7SPK9bIQMPkcOB0lOVK47E fBOCQJwAHRPfRMX828T+Q6VrvzXEGfwnOzhaIUZTtk5d+DpjTsIp4EpN5jDGElfolWOQ K2Tg== MIME-Version: 1.0 X-Received: by 10.66.118.71 with SMTP id kk7mr43623631pab.14.1392265856303; Wed, 12 Feb 2014 20:30:56 -0800 (PST) In-Reply-To: <52fc45e6$0$11128$c3e8da3@news.astraweb.com> References: <85c2698c-d681-4511-b111-bb1e549ece93@googlegroups.com> <52f9c392$0$11128$c3e8da3@news.astraweb.com> <52fc45e6$0$11128$c3e8da3@news.astraweb.com> Date: Thu, 13 Feb 2014 15:30:56 +1100 Subject: Re: PyWart: More surpises via "implict conversion to boolean" (and other steaming piles!) From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392265865 news.xs4all.nl 2839 [2001:888:2000:d::a6]:52168 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66152 On Thu, Feb 13, 2014 at 3:11 PM, Steven D'Aprano wrote: > Think about the difference in difficulty in confirming that > math.sin() of some value x returns the value 0.5, and confirming that > random.random() of some hidden state returns a specific value: > > py> assert math.sin(0.5235987755982989) == 0.5 > > versus: > > py> state = random.getstate() > py> random.seed(12345) > py> assert random.random() == 0.41661987254534116 > py> random.setstate(state) Really, the assertion just requires the setting of the seed and the call to random.random(); the other two are to ensure that you don't fiddle with anything else that's using random.random(). And since random.random() is actually just a bound method of some module-level object, you can actually just create the exact same thing with explicit rather than implicit state: >>> random.Random(12345).random() 0.41661987254534116 Which doesn't tamper with the default object's state. Whether it's a module-level function, a bound method, a closure, or a callable object, a zero-arg function in Python always has some kind of implicit state. The question is, what is it doing with it? In the case of random number generation, maintained state is critical (and making it implicit is usually sufficient); similar with functions like input(), where the return value doesn't really depend on the argument at all [1], and of course anything that iterates over an object is going to need to change some kind of state (either a pointer, or the actual data, depending on whether you're looking at eg iter([1,2,3]).next or [1,2,3].pop). Do you know of any functions in Python that don't use any implicit state and don't take arguments? I can't think of any, but of course that proves nothing. ChrisA [1] input("Enter your name: ") vs input("What is one plus one? ") will probably return different values, but that's playing with humans rather than depending on the value of the argument...