Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64449

Re: Modifying the default argument of function

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'attribute': 0.07; '22,': 0.09; 'function,': 0.09; 'parameter': 0.09; 'variable,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; '[2,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:default': 0.16; 'tuple': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'bit': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'looks': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'once,': 0.31; 'subject:the': 0.34; 'could': 0.34; 'created': 0.35; 'problem.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'accessible': 0.36; 'list': 0.37; 'clear': 0.37; 'easily': 0.37; 'changed': 0.39; 'first': 0.61; 'default': 0.69; 'therefore': 0.72; '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:content-transfer-encoding; bh=bVGdyPX2LHJvnvJWYYLOXsswj8Ayl54f95NZGq1GI74=; b=y2KEd9GLIosiPwuBJb95SspbsjOP6UKyCNfuGFolX3taW2IQBVUkw8d/y5Bfj7AdQH v5K9TINGuXgMA30U3BYjTHhu7oXjSx2+u7wNxTWGkDlFKrQTMeGWInCJ3W7JveQ2vEZJ IcivACTVrBaQqo21vdMA9qCwDmPAocc+uivjclo3iWimwDinXffb5BfXVLjRtMWRBbHz 1fTC5NV4lmhbX/l8uAWNjYue6Pd+tfbhiQUtpTPFFw2+7LQWVxlwsUMxGavrBrcEQjTW /yK+ZO4LF0CUVh/igvAkVBon5lC1KxR8RHmmL1Nw17o9ie+o0lKB+miHcEAWI0u1XafA NnSg==
MIME-Version 1.0
X-Received by 10.68.133.6 with SMTP id oy6mr11227136pbb.153.1390333576920; Tue, 21 Jan 2014 11:46:16 -0800 (PST)
In-Reply-To <52decc31$0$2244$426a74cc@news.free.fr>
References <52dec646$0$2934$426a74cc@news.free.fr> <mailman.5813.1390331956.18130.python-list@python.org> <52decc31$0$2244$426a74cc@news.free.fr>
Date Wed, 22 Jan 2014 06:46:16 +1100
Subject Re: Modifying the default argument of function
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5816.1390333586.18130.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1390333586 news.xs4all.nl 2832 [2001:888:2000:d::a6]:47652
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64449

Show key headers only | View raw


On Wed, Jan 22, 2014 at 6:36 AM, Mû <mu--@melix.net> wrote:
> These were clear and quick answers to my problem. I did not think of this
> possibility: the default argument is created once, but accessible only by
> the function, therefore is not a global variable, whereas it looks like if
> it were at first glance.

You can actually poke at the function a bit and see what's happening.
Try this in the interactive interpreter:

>>> def f(x=[2,3]):
    x.append(1)
    return x

>>> f()
[2, 3, 1]
>>> f()
[2, 3, 1, 1]
>>> f.__defaults__
([2, 3, 1, 1],)

The __defaults__ attribute of a function is a tuple of its parameter
defaults. You can easily see there that the list has changed as you
changed it in the function. You could check it with id() or is, too:

>>> id(f.__defaults__[0])
24529576
>>> id(f())
24529576
>>> f() is f.__defaults__[0]
True

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Modifying the default argument of function Mû <mu--@melix.net> - 2014-01-21 20:11 +0100
  Re: Modifying the default argument of function Chris Angelico <rosuav@gmail.com> - 2014-01-22 06:19 +1100
    Re: Modifying the default argument of function Mû <mu--@melix.net> - 2014-01-21 20:36 +0100
      Re: Modifying the default argument of function Chris Angelico <rosuav@gmail.com> - 2014-01-22 06:46 +1100
        Re: Modifying the default argument of function Asaf Las <roegltd@gmail.com> - 2014-01-21 16:30 -0800
  Re: Modifying the default argument of function Steve Jones <steve@secretvolcanobase.org> - 2014-01-21 19:19 +0000
  Re: Modifying the default argument of function emile <emile@fenx.com> - 2014-01-21 11:20 -0800

csiph-web