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


Groups > comp.lang.python > #6715

Re: scope of function parameters (take two)

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <dan.kluev@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:two': 0.07; 'python': 0.08; '[0,': 0.09; '[0]': 0.09; 'clearly,': 0.09; 'decorator': 0.09; 'mutable': 0.09; 'preserves': 0.09; 'subject:parameters': 0.09; 'pm,': 0.10; '>>>': 0.12; 'def': 0.12; 'received:209.85.214.174': 0.14; 'received:mail- iw0-f174.google.com': 0.14; 'wrote:': 0.14; '*args,': 0.16; 'arg': 0.16; 'nargs': 0.16; 'subject:function': 0.16; 'tue,': 0.17; 'header:In-Reply-To:1': 0.21; 'function': 0.25; 'message- id:@mail.gmail.com': 0.28; 'received:209.85.214': 0.28; 'import': 0.29; 'email name:': 0.30; 'objects.': 0.30; 'pure': 0.32; 'to:addr:python-list': 0.33; "i've": 0.33; 'daniel': 0.34; 'received:google.com': 0.37; 'received:209.85': 0.37; 'subject:: ': 0.38; 'subject: (': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'possibility': 0.40; 'within': 0.60; 'best': 0.60; 'skip:n 30': 0.63; '31,': 0.65; 'making': 0.67; 'intend': 0.73; 'eliminates': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=zQMNlfhj0Efh/Qte09s/l6TNtcLM/7npmf3sID0uVyM=; b=rAyX00hvwCMfjasqDxU1VnJXwwY8wgahN7FGanQEDYyOhJi3vdDrTjzcWGHWEbosT7 IZcfCotSHQC13sCv+uPlkrMz95Z/DbL5LyBJ3OJ1RCPmv/JxkHaXrGNJvCgrNR5HVAip wr+RV6Sykb4Re61GHmOyT1CYgJAuoZ57Zp1bo=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=qQo9HENy7E4YgnFBZE7ejfJsCtiDe6/n2aFUHcfO2Kem99Dk81Jg1JClNGi3J32s/L 1N4X3GNWAX6GDBS91JdhGA7QqOEgDnRINx0f/MsBRPW8ixBbq/NNKr+P9GvobDK7pyJ4 SnSpAONDzw2y00jCPPAdCdFfhIE26uksfhOIY=
MIME-Version 1.0
In-Reply-To <6699AB10-988A-49AD-B7C1-6BAA2CC3D008@mcgill.ca>
References <F8395F78-615E-4FBD-B6FC-1D6173EAEA45@mcgill.ca> <F4EAD1ED-563D-4D6E-A50C-68308A9F26B7@mcgill.ca> <BANLkTin5exEpDkE3on3BAaWwHOjpg_vC8g@mail.gmail.com> <6699AB10-988A-49AD-B7C1-6BAA2CC3D008@mcgill.ca>
Date Tue, 31 May 2011 18:38:40 +1100
Subject Re: scope of function parameters (take two)
From Daniel Kluev <dan.kluev@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.2309.1306827523.9059.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 82.94.164.166
X-Trace 1306827524 news.xs4all.nl 49182 [::ffff:82.94.164.166]:53605
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6715

Show key headers only | View raw


On Tue, May 31, 2011 at 6:17 PM, Henry Olders <henry.olders@mcgill.ca> wrote:
> Clearly, making a copy within the function eliminates the possibility of the
> side effects caused by passing in mutable objects. Would having the
> compiler/interpreter do this automatically make python so much different?

As I've pointed, you can make decorator to do that. Adding @copy_args
to each function you intend to be pure is not that hard.

import decorator
import copy

@decorator.decorator
def copy_args(f, *args, **kw):
    nargs = []
    for arg in args:
        nargs.append(copy.deepcopy(arg))
    nkw = {}
    for k,v in kw.iteritems():
        nkw[k] = copy.deepcopy(v)
    return f(*nargs, **nkw)

@copy_args
def test(a):
    a.append(1)
    return a

>>> l = [0]
>>> test(l)
[0, 1]
>>> l
[0]


>>> inspect.getargspec(test)
ArgSpec(args=['a'], varargs=None, keywords=None, defaults=None)

So this decorator achieves needed result and preserves function signatures.


-- 
With best regards,
Daniel Kluev

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


Thread

Re: scope of function parameters (take two) Daniel Kluev <dan.kluev@gmail.com> - 2011-05-31 18:38 +1100

csiph-web