Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'argument': 0.04; 'attribute': 0.05; 'finally:': 0.05; 'modified': 0.05; 'modify': 0.05; '*args,': 0.07; 'decorator': 0.07; 'dynamically': 0.07; 'elegant': 0.07; 'function,': 0.07; 'none)': 0.07; 'parameter': 0.07; 'seemed': 0.07; '"""return': 0.09; "'''": 0.09; '**kwargs)': 0.09; '**kwargs):': 0.09; '[0,': 0.09; 'defined.': 0.09; 'typeerror:': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'passing': 0.15; 'defs': 0.16; 'framework,': 0.16; 'functools': 0.16; 'given)': 0.16; 'luck!': 0.16; 'param': 0.16; 'simplified': 0.16; 'y):': 0.16; 'module,': 0.17; '(in': 0.18; 'variable': 0.20; 'import': 0.21; 'file:': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'work.': 0.23; 'cc:no real name:2**0': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'fine': 0.28; '"do': 0.29; 'lot.': 0.29; 'testing.': 0.29; 'wrap': 0.29; 'framework': 0.30; 'function': 0.30; 'asking': 0.32; 'could': 0.32; 'print': 0.32; 'function.': 0.33; 'problem': 0.33; 'received:google.com': 0.34; 'add': 0.36; 'but': 0.36; 'received:74.125': 0.36; 'should': 0.36; 'ok,': 0.37; 'does': 0.37; 'usual': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'things': 0.38; 'takes': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'easy': 0.60; 'here': 0.65; 'modify.': 0.84; 'step.': 0.91; 'steps.': 0.91 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:to :cc:content-type; bh=cYE2FNGxPNmJmvZdQj0Kc3QFWKS7srKwgUsbCQKI5dM=; b=GZtt11HNF2daxZU7ouiNYetNMYOkmtTVzCjqCfzqJCaIvGgBHpIpOhY334SYr2XDpJ z445gfE40Vmd7cUCjRLG3qs60TqL6Lkfy7ti+IiCEWMm7dB5aVvCnEiyZKsZS9kc+U9X CSid9MuQDDmeHDVYHM/4cGihrNfcetq3SQlURMeIBJnkV2z/Mk0f9hsCp0PAI2FWpb9P TLUE19lVO+RAaqkez6xnLwOA7Y/lQhTTy8rTTmDjAOKCVxIPd/WbHhLsVz6WwK9XUtFg DClhuHZP4CJPIGdKtG2fSDFH1RD03jSOpOCR6VL+4vrpt+9GUglXIOxZokDBFybAkdVS iqCQ== MIME-Version: 1.0 In-Reply-To: <50bf9764$0$3120$ba620e4c@news.skynet.be> References: <50bf9764$0$3120$ba620e4c@news.skynet.be> Date: Wed, 5 Dec 2012 19:30:48 -0700 Subject: Re: Secretly passing parameter to function From: Modulok To: Olivier Scalbert Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: 117 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354761056 news.xs4all.nl 6881 [2001:888:2000:d::a6]:60852 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34353 > 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 ? Olivier, I would create a partial object using the functools module, but I would also wrap it in a decorator so I could call my functions as usual. Here's an example: # File: framework.py: def do(something): print("Framework in action...") return something() # File: test.py: import functools import framework def pack(func): """Return a function object to be called later.""" def f(*args, **kwargs): """Call the framework passing a partial object to be called.""" print("Wrapper in action...") part = functools.partial(func, *args, **kwargs) return framework.do(part) #<-- Call the simplified function. return f #<-- Return the function object to-be-called. # Usage: Just wrap your defs with the decorator '@pack': @pack def step1(x, y): print(x, y) @pack def step2(a): return sum(a) @pack def step3(): print("Amazing!") # Call your functions as usual e.g: step1(3, 5)... In theory everything should just work. I tested the above example and it seemed to work just fine with my limited testing. Good luck! -Modulok-