Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.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 X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'operator': 0.03; 'builtin': 0.07; 'rewrite': 0.07; 'strings.': 0.07; 'suppose': 0.07; 'python': 0.09; "'w',": 0.09; '(without': 0.09; 'be:': 0.09; 'friday,': 0.09; 'method:': 0.09; 'subclass': 0.09; 'subject:while': 0.09; 'yeah,': 0.09; 'def': 0.10; 'language': 0.14; '(the': 0.15; 'sat,': 0.15; "'e',": 0.16; "'o',": 0.16; "'r',": 0.16; 'declaration,': 0.16; 'folks,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'integers,': 0.16; 'iterable)': 0.16; 'normal,': 0.16; 'omitted,': 0.16; 'permits': 0.16; 'py3': 0.16; 'readable': 0.16; 'syntactic': 0.16; 'syntax,': 0.16; 'variables),': 0.16; 'which,': 0.16; 'string': 0.17; 'wrote:': 0.17; '>>>': 0.18; '(or': 0.18; 'feb': 0.19; 'sort': 0.21; 'java': 0.21; 'together.': 0.21; 'lets': 0.22; 'pos': 0.22; 'skip:_ 20': 0.22; "python's": 0.23; 'this:': 0.23; 'long,': 0.24; 'idea': 0.24; 'header:In- Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'supported': 0.26; 'am,': 0.27; 'restrict': 0.27; 'message-id:@mail.gmail.com': 0.27; 'actual': 0.28; 'chris': 0.28; 'declared': 0.29; 'division': 0.29; 'oop': 0.29; 'restricted': 0.29; 'str': 0.29; 'url:se': 0.29; 'array': 0.29; 'definition': 0.29; 'no,': 0.29; 'objects': 0.29; 'class': 0.29; "i'm": 0.29; 'normally': 0.30; 'function': 0.30; 'stuff': 0.30; 'lists': 0.31; 'code': 0.31; 'johnson': 0.32; 'not.': 0.32; 'int': 0.33; 'right?': 0.33; 'handle': 0.33; 'to:addr:python-list': 0.33; 'members.': 0.33; 'operations': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'list': 0.35; 'sequence': 0.35; 'doing': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'list.': 0.35; 'but': 0.36; 'wanted': 0.36; 'should': 0.36; 'does': 0.37; 'being': 0.37; 'why': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'some': 0.38; 'things': 0.38; 'url:docs': 0.38; 'sure': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'skip:" 10': 0.40; 'your': 0.60; 'first': 0.61; 'bring': 0.62; 'back': 0.62; 'solve': 0.62; 'legal': 0.65; 'useful.': 0.65; 'sum': 0.66; 'fact,': 0.69; '100%': 0.76; 'yourself': 0.77; '2013': 0.84; 'actually,': 0.84; 'differences,': 0.84; 'divide': 0.84; 'overloading': 0.84; 'pike': 0.84; 'thing,': 0.84; 'do:': 0.91; 'rick': 0.91; 'us?': 0.91; 'url:tutorial': 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:content-type; bh=8D/xzar66r3WZqMt61aT/BIXY00luvis02/3Xy0DZlg=; b=HP3UnRevC09zGxTPPpMVBpHdE8PutyGrD3eDuyQkhXDkqg4maiApr48g7gLQjm5D9D u90inC5oKXbxfOIJHk2LkY/vGZbPIesXChMO4s3XqEmyGO9gpv28Tkdy/t1pPI2XF54T 9ogUCiPUS/ZR175wUqm04HBo1jnmDk2C+lZa0QBbjgTrRuuZwul2shKkJHLrI4Tp5C9Y 5gYDDJlpZe/U3VjeBVPgZdsbaTZUJsOhCroDG4SgKHR2/tH9dtlwuMrFEfTy9Vjjbmro v90jzKdOVq/d5bQ63s0EtqxT2nZZxYcOnFMbT/bSz9j5T/d4tqc6Ll/WWnJfbE5OEcY1 uxCg== MIME-Version: 1.0 X-Received: by 10.58.97.165 with SMTP id eb5mr9480458veb.40.1360372646902; Fri, 08 Feb 2013 17:17:26 -0800 (PST) In-Reply-To: <75c82449-773e-4077-a6c9-e9cef08f845f@googlegroups.com> References: <5002a1f9$0$29995$c3e8da3$5496439d@news.astraweb.com> <7b027612-a07e-40f9-8ad2-3e95c5440482@googlegroups.com> <86872ad2-fda0-403b-9f18-d1cb18e41860@t32g2000yqd.googlegroups.com> <50039290$0$29978$c3e8da3$5496439d@news.astraweb.com> <9309333c-13a0-464c-bd94-9c682363b8c9@googlegroups.com> <511516db$0$29969$c3e8da3$5496439d@news.astraweb.com> <62c3e7bb-d023-43b4-b759-f424707fd346@googlegroups.com> <75c82449-773e-4077-a6c9-e9cef08f845f@googlegroups.com> Date: Sat, 9 Feb 2013 12:17:26 +1100 Subject: Re: Implicit conversion to boolean in if and while statements From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 94 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360372655 news.xs4all.nl 6920 [2001:888:2000:d::a6]:32928 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38487 On Sat, Feb 9, 2013 at 11:49 AM, Rick Johnson wrote: > On Friday, February 8, 2013 6:05:54 PM UTC-6, Chris Angelico wrote: >> The sum builtin works happily on any sequence of objects >> that can be added together. It works as an excellent >> flatten() method: >> >> >>> nested_list = [["q"], ["w","e"], ["r","t","u"], ["i","o","p"]] >> >>> sum(nested_list,[]) >> ['q', 'w', 'e', 'r', 't', 'u', 'i', 'o', 'p'] >> >>> nested_list >> [['q'], ['w', 'e'], ['r', 't', 'u'], ['i', 'o', 'p']] > > What the hell? Oh yeah, you must be using pike again. No, if it were pike the list would look like this: > > ({({"q"}), ({"w","e"}), ({"r","t","u"}), ({"i","o","p"})}) > > Of course you'd have to declare it first using an /expanded/ Java syntax: > > nested_list = array(array(string)) Strange... normally I have to actually bring Pike up, no idea why you're doing it for me. But okay. Actually, that's not a declaration, that's an assignment; and in Pike, a 'type' is a thing, same as it is in Python (though not quite). If I were to declare it in Pike, it would be: array(array(string)) nested_list; Though the part inside the parens can be omitted, in which case the array can contain anything, rather than being restricted to strings. In actual fact, Rick, despite your complaints about the syntax, it's able to achieve exactly what you were thinking Python should do: declare an array/list that contains only numbers. > Folks, i couldn't make this stuff up if i wanted to. Go read for yourself if want a few laughs. > > http://pike.lysator.liu.se/docs/tutorial/data_types/container_types.xml Apart from some syntactic differences, which in the scheme of things are pretty trivial, the only difference is that Pike permits (without demanding) you to restrict an array's members. In fact, Pike lets you declare everything as 'mixed' if you like, allowing you to put *anything* into *anywhere*... which, yaknow, is pretty much identical to Python's model (only with declared variables), and takes advantage of the fact that there are no Java-style "unboxed" types (the Pike int is like the Py3 int or the Py2 long, arbitrary precision). >> I'm not sure what your definition of a numeric type is, but I suspect >> that list(str) isn't part of it. > > Of course not. And yet, in both Pike and Python, adding lists/arrays of strings together is not just legal but extremely useful. Do we need to rewrite the sum() function to handle lists instead of letting operator overloading take care of it for us? Suppose I make a string subclass with a division operator: class divisible_string(str): def __truediv__(self,n): n=(len(self)+n-1)//n return [self[pos:pos+n] for pos in range(0,len(self),n)] # and a bunch of functions so operations on a divisible_string return a divisible_string Now, I should be able to divide a string by 2 and get a two-element list. Kinda convenient, would be cool if str supported this, but it's OOP and OOP is all about operator overloading, even if it makes your code less readable - a language isn't 100% OOP unless this sort of thing is normal, right? Suppose also that I have an avg() function that does this: def avg(it): it=iter(it) tot=next(it) pos=-1 for pos,val in enumerate(it): tot+=val return tot/(pos+2) I can take the avg() of a list (or other iterable) of integers, and get back a number. Do I need to rewrite this function to handle my divisible_string? Or do I need to have divisible_string subclass some "averageable" class/interface in order to permit them to be used inside a list that has an avg() method? lst=avg(map(divisible_string,("asdf","qwer","a","z","f","qqqqq","asdfasdfasdf"))) How do you solve the puzzle, Rick? ChrisA