Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Dennis Lee Bieber Newsgroups: comp.lang.python Subject: Re: What is a function parameter =[] for? Date: Thu, 19 Nov 2015 09:05:29 -0500 Organization: IISS Elusive Unicorn Lines: 46 Message-ID: References: <564dbe6b$0$1610$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de N/psTuDhYdSKJgiXN876MATzkgYkIhYu6m+4SVhm//YQ== 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; '"""': 0.05; 'sufficient': 0.05; 'default.': 0.07; 'differently': 0.07; "'default'": 0.09; 'defined,': 0.09; 'message-id:@4ax.com': 0.09; 'modifies': 0.09; 'mutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tutorials,': 0.09; 'python': 0.10; 'language,': 0.11; '2.7': 0.13; 'explicitly': 0.15; 'folks': 0.15; 'thu,': 0.15; 'executed.': 0.16; 'intended.': 0.16; 'list),': 0.16; 'modified.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'shallow': 0.16; 'volatile': 0.16; 'documented': 0.18; 'url:home': 0.18; 'variable': 0.18; 'language': 0.19; '2015': 0.20; 'default,': 0.22; 'function,': 0.22; 'parameter': 0.22; 'installation': 0.23; 'feature': 0.24; 'header:X-Complaints-To:1': 0.26; 'rest': 0.26; '(e.g.': 0.27; 'object,': 0.27; 'function': 0.28; 'values': 0.28; 'actual': 0.28; 'appending': 0.29; 'once,': 0.29; 'sentence': 0.29; 'that.': 0.30; 'call.': 0.30; 'everyone': 0.31; 'anyone': 0.32; 'another': 0.32; 'especially': 0.32; 'generally': 0.32; 'surprised': 0.33; 'definition': 0.34; 'list': 0.34; 'nov': 0.35; 'knowledge': 0.35; 'something': 0.35; 'item': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; '(and': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:org': 0.37; 'manual': 0.38; 'or,': 0.38; 'means': 0.39; 'test': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'behavior': 0.61; 'body': 0.61; 'default': 0.61; 'documents': 0.61; 'one-time': 0.66; 'skip:\xe2 10': 0.70; 'surprise': 0.72; '7.6': 0.84; '>of': 0.84; '>you': 0.84; 'cabal': 0.84; 'hunt': 0.84; 'local,': 0.84; 'presumably': 0.84; '8bit%:33': 0.91; 'dennis': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: adsl-108-79-217-215.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES 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:99059 On Thu, 19 Nov 2015 13:19:25 +0000, BartC declaimed the following: >You presumably think differently because you have some inside knowledge >of how Python works, and know that that [] undergoes a one-time >assignment to a local, persistent 'default' variable where it's value >can indeed by changed. (Thanks to another Python feature where an >assignment is a very shallow copy of an object.) And it is that volatile >variable that is the actual default. > >But not everyone is going to know that. If one has not taken time to read the language reference manual and/or tutorials, one should expect to be taken by surprise. This behavior has existed in Python from the beginning... That's over 20 years now. It is DOCUMENTED -- in documents that come with every standard installation of the language, not something one needs to hunt down. It should not be a surprise to anyone who has taken the time to read and understand the documentation -- or, at least, has searched the documentation once they have been surprised (I'll concede that one read-through at the start may not be sufficient ; one may need to encounter a behavior before the documentation for it makes sense). Python 2.7 LRM section 7.6 Function Definitions: """ Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same “pre-computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g.: """ (and the first sentence is even BOLDED) But in no way is this "inside knowledge". There is no cabal of folks hoarding arcana from the rest of the public. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/