Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34312 > unrolled thread
| Started by | Olivier Scalbert <olivier.scalbert@algosyn.com> |
|---|---|
| First post | 2012-12-05 19:50 +0100 |
| Last post | 2012-12-06 13:14 -0800 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | Olivier Scalbert <olivier.scalbert@algosyn.com> |
|---|---|
| Date | 2012-12-05 19:50 +0100 |
| Subject | Secretly passing parameter to function |
| Message-ID | <50bf9764$0$3120$ba620e4c@news.skynet.be> |
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 ?
Thanks,
Olivier
[toc] | [next] | [standalone]
| From | Chris Kaynor <ckaynor@zindagigames.com> |
|---|---|
| Date | 2012-12-05 11:21 -0800 |
| Message-ID | <mailman.518.1354735308.29569.python-list@python.org> |
| In reply to | #34312 |
[Multipart message — attachments visible in raw view] — view raw
On Wed, Dec 5, 2012 at 10:50 AM, Olivier Scalbert <
olivier.scalbert@algosyn.com> 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)
>
> Probably the easiest solution would be to use functools.partial to create
a proxy function, as follows:
import functools
do(functools.partial(step1, param=None))
do(functools.partial(step1, param=[0,1,5]))
do(step2)
Effectively what functools.partial does is returns a new function with the
arguments defined in the constructor. It looks something like:
def partial(func, *args, **kwargs):
def newFunc(*cargs, **ckwargs):
return func(*args+cargs, **kwargs+ckwargs)
return newFunc
Note, that implementation of partial may not exactly match the semantics of
the real thing, and is untested and may just flat out not work...I'd
recommend using the real one instead.
In this case, it will produce a function that really takes no additional
arguments, as all arguments are defined as part of the creation, meaning
the new function will match the requirements by the framework of taking 0
arguments, despite the actual function taking and receiving one keyword
argument (param).
There is one caveat of using functools.partial: for positional arguments,
you cal only fill the arguments in left-to-right order; you cannot specify
the second argument of a list, other than specifying it by name. For
example, for the function "def test(a, b, c)", you can specify arguments
"a", "a and b", or "a and b and c" by position, but you cannot specify only
the arguments "b", "c", "a and c", or "b and c" by position. You can,
however, specify them by name, as keyword-arguments.
An alternative approach to the problem is to either use lamdas or write a
specialized wrapper like my example partial above to specify the arguments.
These eliminate the restriction on argument order for partial.
An example for lamdas would be:
do(lambda: step1(param=None))
do(lambda: step1(param=[0,1,5]))
do(step2)
This works much the same way as partial, in that it creates a new, unnamed
function which specifies the arguments.
> 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 ?
>
> Thanks,
>
>
> Olivier
>
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-12-05 14:21 -0500 |
| Message-ID | <mailman.519.1354735311.29569.python-list@python.org> |
| In reply to | #34312 |
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
[toc] | [prev] | [next] | [standalone]
| From | Modulok <modulok@gmail.com> |
|---|---|
| Date | 2012-12-05 19:30 -0700 |
| Message-ID | <mailman.540.1354761056.29569.python-list@python.org> |
| In reply to | #34312 |
> 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-
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2012-12-06 13:13 -0800 |
| Message-ID | <7xpq2m28ai.fsf@ruckus.brouhaha.com> |
| In reply to | #34312 |
Olivier Scalbert <olivier.scalbert@algosyn.com> writes: > # We ask the framework to do some work. > do(step1, param = None) from functools import partial do(partial(step1, param = None))
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2012-12-06 13:14 -0800 |
| Message-ID | <7xlida2892.fsf@ruckus.brouhaha.com> |
| In reply to | #34430 |
Paul Rubin <no.email@nospam.invalid> writes: > from functools import partial > do(partial(step1, param = None)) Or more directly: do(lambda: step1(param = None))
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web