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


Groups > comp.lang.python > #3958

Re: Function __defaults__

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ken@seehart.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'defaults': 0.07; 'python': 0.07; '>>>>': 0.09; 'attribute': 0.09; 'subject:Function': 0.09; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; '(until': 0.16; 'around,': 0.16; 'f()': 0.16; 'patching': 0.16; 'received:192.168.1.20': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'header:In-Reply-To:1': 0.22; 'previously': 0.22; 'documented': 0.23; 'ken': 0.23; 'expect': 0.26; '(in': 0.27; 'function': 0.27; 'changing': 0.29; 'probably': 0.30; 'luck,': 0.31; 'url:library': 0.31; 'to:addr :python-list': 0.32; '...': 0.32; 'url:docs': 0.33; 'received:192.168.1': 0.34; 'received:192': 0.34; 'header:User- Agent:1': 0.35; 'quite': 0.36; 'received:192.168': 0.37; 'url:python': 0.37; 'steven': 0.38; 'url:org': 0.38; 'stable': 0.39; 'work?': 0.39; 'to:addr:python.org': 0.39; 'would': 0.40; "it's": 0.40; 'best': 0.60; 'safe': 0.65; 'fashion': 0.65; 'url:3': 0.68; 'url:0': 0.69; 'guaranteed': 0.72; 'technique.': 0.84
Date Sun, 24 Apr 2011 10:07:02 -0700
From Ken Seehart <ken@seehart.com>
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9
MIME-Version 1.0
To python-list@python.org
Subject Re: Function __defaults__
References <4db3f444$0$29978$c3e8da3$5496439d@news.astraweb.com>
In-Reply-To <4db3f444$0$29978$c3e8da3$5496439d@news.astraweb.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.798.1303666106.9059.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 82.94.164.166
X-Trace 1303666106 news.xs4all.nl 41113 [::ffff:82.94.164.166]:60892
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3958

Show key headers only | View raw


On 4/24/2011 2:58 AM, Steven D'Aprano wrote:
> Consider this in Python 3.1:
>
>
>>>> def f(a=42):
> ...     return a
> ...
>>>> f()
> 42
>>>> f.__defaults__ = (23,)
>>>> f()
> 23
>
>
> Is this an accident of implementation, or can I trust that changing
> function defaults in this fashion is guaranteed to work?

This is documented in python 3, so I would expect it to be stable (until 
python 4, that is)
http://docs.python.org/py3k/whatsnew/3.0.html#operators-and-special-methods
http://docs.python.org/py3k/library/inspect.html#types-and-members

The f.__defaults__ attribute was previously known as f.func_defaults (in 
python 2.x), which has been around, documented and stable for quite a while.

So it's probably just as safe as any other monkey patching technique. :)

Best of luck,
Ken

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


Thread

Function __defaults__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-24 09:58 +0000
  Re: Function __defaults__ Terry Reedy <tjreedy@udel.edu> - 2011-04-24 12:48 -0400
  Re: Function __defaults__ Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-04-24 13:02 -0400
  Re: Function __defaults__ Daniel Kluev <dan.kluev@gmail.com> - 2011-04-25 04:18 +1100
  Re: Function __defaults__ Ken Seehart <ken@seehart.com> - 2011-04-24 10:07 -0700
    Re: Function __defaults__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-25 02:00 +0000
  Re: Function __defaults__ Ken Seehart <ken@seehart.com> - 2011-04-24 14:44 -0700
  Re: Function __defaults__ Daniel Kluev <dan.kluev@gmail.com> - 2011-04-25 08:47 +1100
  Re: Function __defaults__ Terry Reedy <tjreedy@udel.edu> - 2011-04-24 17:53 -0400
  Re: Function __defaults__ Ken Seehart <ken@seehart.com> - 2011-04-24 14:54 -0700
  Re: Function __defaults__ "Colin J. Williams" <cjw@ncf.ca> - 2011-04-25 07:59 -0400
  Re: Function __defaults__ Ken Seehart <ken@seehart.com> - 2011-04-25 05:30 -0700
  Re: Function __defaults__ "Colin J. Williams" <cjw@ncf.ca> - 2011-04-25 10:24 -0400

csiph-web