Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: What is a function parameter =[] for? Date: Tue, 24 Nov 2015 11:04:05 -0700 Lines: 73 Message-ID: References: <564df258$0$1604$c3e8da3$5496439d@news.astraweb.com> <8601c9af-a7d9-4642-ba1c-8edd1e4c3390@googlegroups.com> <56546985.8060704@rece.vub.ac.be> <56547337.4000709@rece.vub.ac.be> <56547C53.8030407@rece.vub.ac.be> <5654864E.2070906@rece.vub.ac.be> <56549324.8020607@rece.vub.ac.be> <56549F15.40206@rece.vub.ac.be> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de wORnp/u4xv3hIYtcnKpOJgrKNH/KHjQ0mlJJ6POJENww== 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; 'defines': 0.07; 'skip:l 60': 0.07; '0),': 0.09; '3),': 0.09; '5),': 0.09; 'definition,': 0.09; 'indeed,': 0.09; 'literal': 0.09; 'optional': 0.09; 'python:': 0.09; 'separating': 0.09; 'stringio': 0.09; '1),': 0.16; '2),': 0.16; '24,': 0.16; '4),': 0.16; '6),': 0.16; 'enumerates': 0.16; 'grounds': 0.16; 'language:': 0.16; 'lexical': 0.16; 'literal.': 0.16; 'literals': 0.16; 'notations': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'thread.': 0.16; 'token,': 0.16; 'tokenize': 0.16; 'wrote:': 0.16; 'byte': 0.18; '>>>': 0.20; '2015': 0.20; 'constant': 0.22; 'am,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'followed': 0.27; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'specifically': 0.28; 'values': 0.28; 'argue': 0.29; 'types.': 0.29; 'starts': 0.29; 'code': 0.30; 'skip:[ 10': 0.31; 'related': 0.32; 'point': 0.33; 'url:python': 0.33; 'point,': 0.33; 'steven': 0.33; 'though.': 0.33; 'similar': 0.33; 'tue,': 0.34; 'previous': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'confirmed': 0.35; 'displays': 0.35; 'nov': 0.35; 'too': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'rather': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'url:3': 0.60; 'more': 0.63; 'limited.': 0.67; 'therefore': 0.67; '10:32': 0.84; 'pardon': 0.84; 'schreef': 0.84; 'to:name:python': 0.84; 'url:reference': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=/k3AlE987wZGOL3enz1Mkw1Ys7evEPkP/2WG2hhmCk4=; b=aOit8Qe2IRUs8mdnTG+n5cTngnzxyuEVoUyFLyv3q6M3p3N1DQzgaiOSmXClhyFTHs 2kwt+qq7gW6abh+w+ed52ON1qb0N0AjV9kL8zD2Qrf2kGPvIYnHZbIjgJQndu0y7eV15 QHThq8+hDUR5R2GLUfO+tfSo2NDjxy6KvD6EAQOX0KbzeUBjE5OJBixHBaD6jwNmXPGV tKIGhWs8oMOz44ed+Rhbcv5RsSo0hHXnZARvLTIkCnZoNflxd3Uw+451EBS8rWb9VyM9 utxoDFivS2LlqUNgXFhLZj1ljsYtnVJKq6d79GkZlaF09y/lSceCqHNVFapP4XwCI5Kw 05wQ== X-Received: by 10.50.147.36 with SMTP id th4mr17748910igb.68.1448388284423; Tue, 24 Nov 2015 10:04:44 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99379 On Tue, Nov 24, 2015 at 10:53 AM, Ian Kelly wrote: > On Tue, Nov 24, 2015 at 10:32 AM, Antoon Pardon > wrote: >> Op 24-11-15 om 17:56 schreef Ian Kelly: >> >>> >>>> So on what grounds would you argue that () is not a literal. >>> >>> This enumerates exactly what literals are in Python: >>> >>> https://docs.python.org/3/reference/lexical_analysis.html#literals >>> >>> I think it's a rather pedantic point, though. How are nuances of the >>> grammar at all related to user expectations? >>> >> >> I think that enumaration is too limited. The section starts with: >> >> Literals are notations for constant values of some built-in types. >> >> () satisfies that definition, which is confirmed by the byte code >> produced for it. > > Literals are a type of lexical token. All of the literals shown in > that section are, indeed, tokens. Now I would point you to the grammar > specification: > > https://docs.python.org/3/reference/grammar.html > > And specifically the "atom" rule, which defines both list displays and > list comprehensions (as well as literals) as being atoms. > Specifically, it parses () as the token '(', followed by an optional > yield_expr or testlist_comp, followed by the token ')'. In no way is > that a single token, nor therefore a literal. In case reading the grammar doesn't convince, we can also get this result from playing with the language: >>> import tokenize >>> from io import StringIO >>> list(tokenize.generate_tokens(StringIO('()').readline)) [TokenInfo(type=52 (OP), string='(', start=(1, 0), end=(1, 1), line='()'), TokenInfo(type=52 (OP), string=')', start=(1, 1), end=(1, 2), line='()'), TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='')] Two separate tokens of the OP type. >>> list(tokenize.generate_tokens(StringIO('42').readline)) [TokenInfo(type=2 (NUMBER), string='42', start=(1, 0), end=(1, 2), line='42'), TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='')] One token, of a literal type. >>> list(tokenize.generate_tokens(StringIO('(1,2,3)').readline)) [TokenInfo(type=52 (OP), string='(', start=(1, 0), end=(1, 1), line='(1,2,3)'), TokenInfo(type=2 (NUMBER), string='1', start=(1, 1), end=(1, 2), line='(1,2,3)'), TokenInfo(type=52 (OP), string=',', start=(1, 2), end=(1, 3), line='(1,2,3)'), TokenInfo(type=2 (NUMBER), string='2', start=(1, 3), end=(1, 4), line='(1,2,3)'), TokenInfo(type=52 (OP), string=',', start=(1, 4), end=(1, 5), line='(1,2,3)'), TokenInfo(type=2 (NUMBER), string='3', start=(1, 5), end=(1, 6), line='(1,2,3)'), TokenInfo(type=52 (OP), string=')', start=(1, 6), end=(1, 7), line='(1,2,3)'), TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='')] Two tokens for the parentheses, plus three literal tokens for the ints, plus two more tokens for the separating commas. Definitely not a literal. Credit to Steven for using this approach to make a similar point about literals in a previous thread.