Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'attribute': 0.07; 'method.': 0.07; 'string': 0.09; 'integral': 0.09; 'string;': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '(unlike': 0.16; '(when': 0.16; '*you*': 0.16; 'enough.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instead:': 0.16; 'magic': 0.16; 'nearest': 0.16; 'operators,': 0.16; 'partly': 0.16; 'subtract': 0.16; 'syntactic': 0.16; 'truncates': 0.16; 'typeerror:': 0.16; 'so.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'all,': 0.19; 'module': 0.19; 'feb': 0.22; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'bytes': 0.24; 'instead.': 0.24; 'string,': 0.24; 'cc:2**0': 0.24; 'define': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'array': 0.29; "doesn't": 0.30; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; '"",': 0.31; 'operators': 0.31; 'skip:7 10': 0.31; 'file': 0.32; 'checked': 0.32; 'languages': 0.32; '(most': 0.33; 'implemented': 0.33; 'sense': 0.34; 'could': 0.34; 'basic': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'explains': 0.36; 'functions.': 0.36; 'done': 0.36; 'doing': 0.36; 'method': 0.36; 'possible': 0.36; 'two': 0.37; 'sometimes': 0.38; 'pm,': 0.38; 'rather': 0.38; 'recent': 0.39; 'expect': 0.39; 'does': 0.39; 'either': 0.39; 'ian': 0.60; 'number,': 0.60; 'then,': 0.60; 'numbers': 0.61; 'matter': 0.61; 'more': 0.64; 'counts': 0.83; 'divide': 0.84; 'each,': 0.84; 'functions:': 0.84; 'whereas': 0.91; 'to:none': 0.92 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=eG9DjmBwdWNw/yCXEMaE7hDUYAv1QtVEi70UlmnvwCM=; b=vLc5UCQw4ZAoOtfmMHbsvkoRe9wR6LPv3BsOrjxqtZEcGZ8X04zDg0mwmGDvwJVZrK ybbk690oDYb5LxYWX6aB+JpJObV6IK8wo4RFASdaQJvz1w5TR9WL44peKuF0UZELiod1 LjqCeL562ddjMyy8A+EOEPg0Ie0myiYRdUa2AUI4umSSY7f/pB7H1y4mzXFWVcoY/HWf 0PJhR+8rQlQXp8BFjGdsPOVZjiKM+K98g7vVIWQ+zDexE+nfho6LYWvw0Xv2yHS7IDzw zP8ry9xhPQC/qua/XoQtW9GX62LfQpnhkM8qsiAFFusvsYNDSv+sWZtzUVM1CP1R6Xvb XbMQ== MIME-Version: 1.0 X-Received: by 10.68.162.66 with SMTP id xy2mr20240732pbb.46.1392549882643; Sun, 16 Feb 2014 03:24:42 -0800 (PST) In-Reply-To: <87wqgvgwhh.fsf@elektro.pacujo.net> References: <13208de8-0f85-4e60-b059-dc087c8fda41@googlegroups.com> <917ede6d-db7c-4a8c-8203-27677283776b@googlegroups.com> <871tz5piy0.fsf@elektro.pacujo.net> <87vbwho1i0.fsf@elektro.pacujo.net> <8761ofiio9.fsf@elektro.pacujo.net> <87wqgvgwhh.fsf@elektro.pacujo.net> Date: Sun, 16 Feb 2014 22:24:42 +1100 Subject: Re: Explanation of list reference 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: 75 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392549885 news.xs4all.nl 2869 [2001:888:2000:d::a6]:49837 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66525 On Sun, Feb 16, 2014 at 9:52 PM, Marko Rauhamaa wrote: > Ian Kelly : > >>>>> (1).__str__() >> '1' > > Fair enough. > > The syntactic awkwardness, then, explains why numbers don't have an > evolved set of methods (unlike strings). No; it's more that numbers are more often used with either operators or polymorphic functions. Yes, you can add strings together with +, but with numbers, you also subtract them, multiply them (okay, you can multiply a string by a number, but that kinda counts one to each), and divide them (some languages let you divide a string by a string, but Python does that with the .split() method). There are only two types of string, and arguably one of them is more an array of bytes than it is a string; but with numbers, you have int/float/complex/Fraction/Decimal, and rather than create methods that have to be implemented by each, there are stand-alone functions instead. Strings get methods: >>> "a b c d".count(" ") 3 >>> "asdf".capitalize() 'Asdf' >>> "asdf".center(20) ' asdf ' Numbers get functions: >>> math.sqrt(100) 10.0 >>> math.sqrt(100.0) 10.0 >>> math.log(1234) 7.1180162044653335 Sometimes the line is blurred: >>> help(math.trunc) Help on built-in function trunc in module math: trunc(x) Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method. Which could have been done as x.trunc() instead: >>> 0o1.__trunc__ >>> 1.0.__trunc__ >>> (1+0j).__trunc__ Traceback (most recent call last): File "", line 1, in (1+0j).__trunc__ AttributeError: 'complex' object has no attribute '__trunc__' >>> math.trunc(1+0j) Traceback (most recent call last): File "", line 1, in math.trunc(1+0j) TypeError: type complex doesn't define __trunc__ method But in a lot of cases, it makes good sense to have a single function that can take many types of number (since, after all, it's possible to define a lot of functions in terms of the basic operators), whereas doing them as methods would entail duplicating code. Partly it's just a matter of expectations. People expect strings to have more methods and numbers to use more operators, so a string method is discoverable and an int method is less so. (When was the last time *you* checked to see what methods an int has?) ChrisA