Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: What is a function parameter =[] for? Date: Fri, 20 Nov 2015 06:19:06 +1100 Lines: 53 Message-ID: References: <564dbe6b$0$1610$c3e8da3$5496439d@news.astraweb.com> <564df258$0$1604$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de jbo0CtNt8ErxoAiZL9ONqwIT/oxRdssoOl0LddoCTm/w== 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; 'compiler': 0.05; 'none:': 0.05; 'default.': 0.07; 'raises': 0.07; 'cc:addr:python-list': 0.09; '"if': 0.09; "ain't": 0.09; 'func': 0.09; 'modulo': 0.09; 'of)': 0.09; 'semantics': 0.09; 'def': 0.13; 'translate': 0.15; 'argument': 0.15; '42,': 0.16; 'constructs': 0.16; 'defaults.': 0.16; 'fine;': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'object).': 0.16; 'omitted,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'scope.': 0.16; 'substitute': 0.16; 'wrote:': 0.16; 'string': 0.17; 'integer': 0.18; 'module,': 0.18; 'nested': 0.18; 'language': 0.19; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'saying': 0.22; "aren't": 0.22; 'function,': 0.22; 'int,': 0.22; 'am,': 0.23; "python's": 0.23; 'import': 0.24; 'examples': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'sense': 0.26; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; '(it': 0.29; 'implicitly': 0.29; 'anywhere': 0.30; 'error.': 0.31; 'another': 0.32; 'knows': 0.32; 'run': 0.33; 'choices': 0.33; 'steven': 0.33; 'values.': 0.33; 'definition': 0.34; 'languages': 0.34; 'lists': 0.34; 'received:google.com': 0.35; 'done': 0.35; 'nov': 0.35; 'stable': 0.35; 'knowledge': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'late': 0.38; "won't": 0.38; 'received:209': 0.38; 'stuff': 0.38; 'several': 0.38; 'mean': 0.38; 'whatever': 0.39; 'called': 0.40; 'your': 0.60; 'default': 0.61; 'is.': 0.63; 'different': 0.63; '20,': 0.66; 'binding': 0.66; 'therefore': 0.67; 'directly.': 0.76; 'gain': 0.82; '"simply': 0.84; 'chrisa': 0.84; 'shouting,': 0.84; 'simple:': 0.84; 'to:none': 0.91; 'choices.': 0.91 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=XMDtIYAI8kwnvKsb+ftYDkh1orACM8qhjvgYGxkdn1o=; b=XVRjjw19MpRvHneJHDQ6oEBnGiT+PD4/OPvX7HAUq8Wbi5noCdoM8rzs4Lo8KUvJks vh76x5J7e8DyzWVdWB1nI2Tup2GyMlaV2xC2J+U81JymaR88pM3D7xGsO4rGonJ2VAGm JvMzBOdfyAJa6y8ZEfHYXU6F7Gwxvy8d1UH52MWk88tyg2z0hrp8+je17IJGZz//H4w/ EFGzb3JSV+iFaLupyK3zPd38CQcRFxrypuX3X5hT+1CWcobLnKvvnbzSAQrbrrNW/uKc I2Wvv6ll01A2vz4N49pPP7PMP41AyJSxpPsRiyyLTL7tRqj1/kxxfH3z+fnlIU1uK/tx 5eAg== X-Received: by 10.50.225.38 with SMTP id rh6mr15217523igc.13.1447960746230; Thu, 19 Nov 2015 11:19:06 -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:99086 On Fri, Nov 20, 2015 at 5:19 AM, BartC wrote: > Yes. In the languages I create, pretty much everything is mutable, provided > it can supply an l-value. Constructs such as those for empty lists ([] in > Python, () in mine) aren't l-values. > > But it doesn't apply default values. The language is very different however, > because the byte-code compiler always has full knowledge of functions that > are called directly. Then it knows when an argument is omitted, and can > simply substitute the expression used as the default. In other words, your language uses late binding by default. That's fine; it's a design choice that you've made one way and Python's made the other way. It's on par with design choices like whether adding a string and an integer implicitly stringifies the int, or raises an error. I know several good languages that have taken each of those choices. But every choice has consequences. Steven gave you a few examples of the consequences of late-binding default arguments. For example, can you translate this into (one of) your language(s), and explain the semantics of the late binding? # scope 1: can be a module, an inner function, whatever y = 42 def func(x=y): return x def change_y(): global y # affect the one in this scope y = 28 # scope 2: another module/function/etc from scope1 import func # gain access, however that's done change_y() y = 7 func() Should this return 7, 28, or 42? Early binding demands 42, which is nice and simple: you don't need to look anywhere outside the function's own definition to grok its defaults. The default is stable (it won't change from one run to another - it'll always be the same object). Scope-recognizing late binding would use 28; it re-evaluates the expression 'y' in its original scope. This one makes the most sense to me, of all late-bind semantics; it also happens to be the same semantics as you get if you do the classic "if x is None: x=y" late-bind in Python, modulo the effect of the nested scope. But you're saying that it "simply substitute[s] the expression", which would mean that "func()" is exactly the same as "func(y)". A function default argument is therefore able to STEAL STUFF FROM THE CALLER'S SCOPE. Sorry for shouting, but if that ain't bizarre, I don't know what is. ChrisA