Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed1a.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'argument': 0.05; '[0]': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '"x"': 0.16; 'argument.': 0.16; 'declared': 0.16; 'message-id:@4ax.com': 0.16; 'mutable': 0.16; 'no...': 0.16; 'overloaded': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wed,': 0.18; 'passing': 0.19; 'url:home': 0.24; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'code': 0.31; '+0100,': 0.31; '>>>>': 0.31; 'implemented': 0.33; 'something': 0.35; 'curious': 0.36; 'charset:us-ascii': 0.36; 'implement': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'previous': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'forum': 0.61; 'default': 0.69; 'saw': 0.77; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Curious function argument Date: Wed, 12 Nov 2014 09:18:30 -0500 Organization: IISS Elusive Unicorn References: <54635bce$0$2384$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-108-79-223-190.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.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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415801914 news.xs4all.nl 2855 [2001:888:2000:d::a6]:38526 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 3359 X-Received-Body-CRC: 3862729768 Xref: csiph.com comp.lang.python:80605 On Wed, 12 Nov 2014 14:07:14 +0100, "ast" declaimed the following: >Hello > >I saw in a code from a previous message in this forum >a curious function argument. > >def test(x=[0]): > print(x[0]) ## Poor man's object > x[0] += 1 > >>>> test() >0 >>>> test() >1 >>>> test() >2 >>>> > >I understand that the author wants to implement a global >variable x . It would be better to write 'global x' inside the >function. > No... The author implemented a mutable DEFAULT argument. The argument can still be overloaded by passing something else into the function. y = [0] z = 234 test(y) test(z) This would not work if one instead declared a global "x" -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/