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


Groups > comp.lang.python > #34316

Re: Secretly passing parameter to function

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
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; 'argument': 0.04; 'attribute': 0.05; 'finally:': 0.05; 'modified': 0.05; 'modify': 0.05; 'dynamically': 0.07; 'elegant': 0.07; 'function,': 0.07; 'none)': 0.07; 'parameter': 0.07; 'suppose': 0.07; "'''": 0.09; '[0,': 0.09; 'buttons': 0.09; 'callable': 0.09; 'defined.': 0.09; 'flush(self):': 0.09; 'func': 0.09; 'parameter.': 0.09; 'typeerror:': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'gui': 0.11; 'skip:f 30': 0.15; 'closure,': 0.16; 'framework,': 0.16; 'functools': 0.16; 'given)': 0.16; 'param': 0.16; 'wrote:': 0.17; 'instance,': 0.17; '(in': 0.18; 'variable': 0.20; 'import': 0.21; 'cc:2**0': 0.23; 'work.': 0.23; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'question': 0.27; '"do': 0.29; 'lot.': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'framework': 0.30; 'function': 0.30; 'asking': 0.32; 'print': 0.32; 'problem': 0.33; 'pm,': 0.35; 'add': 0.36; 'but': 0.36; 'ok,': 0.37; 'does': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'things': 0.38; 'takes': 0.39; 'received:192': 0.39; 'called': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'easy': 0.60; 'real': 0.61; 'different': 0.63; 'here': 0.65; 'header :Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'calls,': 0.84; 'modify.': 0.84; 'trick,': 0.84; 'step.': 0.91; 'steps.': 0.91
Date Wed, 05 Dec 2012 14:21:21 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1
MIME-Version 1.0
To Olivier Scalbert <olivier.scalbert@algosyn.com>
Subject Re: Secretly passing parameter to function
References <50bf9764$0$3120$ba620e4c@news.skynet.be>
In-Reply-To <50bf9764$0$3120$ba620e4c@news.skynet.be>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:k2U0sUGTnX/IciLAfIbjm/tkk2kjo3mz25/Z3eOXqjs BszHzpNl2SBiI87DeA4Y8mPD+GEpNAIbLQ1geg1frA2X60ZBsy f0bbfUGeOfX45QUK+CSLOyciMEX05JfWeoMSQyeu2gNnwD2SL7 bskcWV9u9CbUWPsFyGaDDor8s4Ljuf+GhZFqpfz2riyWQ6gQdW hxbCs6RGRpmeE5nR9XjldhnSUVztEtRjXz5ZR8Zzs+nMSEYEij Yu9H2mO8C38xl1wTQendRszpzKe+3X76XD3upwoEhwwTxtiWbI sT9jHe1Q4WikP4nOwVmmk3sMvkgcM9VRq4+orPLIhSv0dSC8A= =
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To d@davea.name
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.519.1354735311.29569.python-list@python.org> (permalink)
Lines 112
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1354735311 news.xs4all.nl 6915 [2001:888:2000:d::a6]:43423
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:34316

Show key headers only | View raw


On 12/05/2012 01:50 PM, Olivier Scalbert wrote:
> Hi all !
>
> I have a problem that is not easy to explained, so I have tried to
> reduce it a lot.
>
> We are using a framework, that we can not modify.
>
> in framework.py:
> def do(something):
>    '''
>    Here we are in a framework that can not be modified ...
>    It does a lot of things
>
>    and finally:
>    '''
>    something()
>
> in test.py:
> from framework import *
>
> def step1():
>    print "Do step1"
>
> def step2():
>    print "Do step2"
>
>
> # We ask the framework to do some work.
> do(step1)
> do(step2)
> do(step3)
>
>
> We are writing step1, step2, ... and asking the framework to process
> them.
> Everything is ok, until we want to add a parameter to some steps.
> We want to be able to do that:
>
> in test.py:
> from framework import *
>
> def step1(param):
>    print "Do step1 with param"
>
> def step2():
>    print "Do step2"
>
>
> # We ask the framework to do some work.
>
> do(step1, param = None)
> do(step1, param = [0, 1, 5]) # again
> do(step2)
>
> Of course it does not work ...
> TypeError: do() takes exactly 1 argument (2 given)
>
> And we can not modify the framework (in which "do" is defined.
>
> One solution would be to use a global variable that can be set before
> each step. But it is not very elegant ...
>
> One other approach would be to add dynamically an attribute the the
> step1 function, and retrieve it inside the function, but it is perhaps
> overkill.
>
> Do you have some ideas ?
>
Other approaches are  lamba, default-argument trick, a function closure,
a callable class instance, and functools.partial.

The real question you have to ask is what is the scope AND LIFETIME of
this parameter.  Suppose you want to want to have five of these same
calls, with five different parameters?  (Example, a GUI where you have a
single function which might be called on an event of any of five buttons
-- you want to pass the button-object to the function)


import functools

def step1(param):
    print "Do step1 with param", param

def step2():
    print "Do step2"

class Framework:
    def __init__(self):
        self.pending = []
    def do(self, func):
        print "current", self.pending
        self.pending.append(func)
    def flush(self):
        for func in self.pending:
            func()


frame = Framework()
frame.do(step2)
frame.do(step2)
frame.do(step2)
frame.do(functools.partial(step1, 45))

frame.flush()



-- 

DaveA

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


Thread

Secretly passing parameter to function Olivier Scalbert <olivier.scalbert@algosyn.com> - 2012-12-05 19:50 +0100
  Re: Secretly passing parameter to function Chris Kaynor <ckaynor@zindagigames.com> - 2012-12-05 11:21 -0800
  Re: Secretly passing parameter to function Dave Angel <d@davea.name> - 2012-12-05 14:21 -0500
  Re: Secretly passing parameter to function Modulok <modulok@gmail.com> - 2012-12-05 19:30 -0700
  Re: Secretly passing parameter to function Paul Rubin <no.email@nospam.invalid> - 2012-12-06 13:13 -0800
    Re: Secretly passing parameter to function Paul Rubin <no.email@nospam.invalid> - 2012-12-06 13:14 -0800

csiph-web