FAQ

fdu.xiaojf at gmail.com wrote:

What I want is, the value of i should be bounded to the anonymous
function. And the output should like this: ...
How to achieve this?
This doesn't answer your question (others have), but another (perhaps
clearer) way to do such things is something like

class MyFunc(object):
"""A function object."""
def __init__(self, val):
self.val = val

def __call__(self):
"""Return value squared"""
return self.val**2

a = []
for i in range(4):
a.append(MyFunc(i))

for f in a:
f()


Jeremy

Search Discussions

  • Duncan Booth at Jul 16, 2007 at 12:06 pm

    "fdu.xiaojf at gmail.com" wrote:

    What I want is, the value of i should be bounded to the anonymous
    function. And the output should like this:

    for f in a:
    print f()

    1
    4
    9

    How to achieve this?
    Use default arguments when defining your function: default arguments are
    evaluated when the function is defined, bound arguments get the current
    value of the variable at the point when it is referenced during the call.

    Also, since you give your function a name anyway ('b') you might as well
    use a named function as being clearer than using lambda:
    a = []
    for i in range(4):
    def b(i=i):
    return i**2
    a.append(b)

    for f in a:
    f()



    1
    4
    9
    >>>
  • Fdu Xiaojf at Jul 16, 2007 at 1:03 pm
    Hi,
    Duncan Booth wrote:
    "fdu.xiaojf at gmail.com" wrote:
    What I want is, the value of i should be bounded to the anonymous
    function. And the output should like this:

    for f in a:
    print f()

    1
    4
    9

    How to achieve this?
    Use default arguments when defining your function: default arguments are
    evaluated when the function is defined, bound arguments get the current
    value of the variable at the point when it is referenced during the call.

    Also, since you give your function a name anyway ('b') you might as well
    use a named function as being clearer than using lambda:
    a = []
    for i in range(4):
    def b(i=i):
    return i**2
    a.append(b)

    for f in a:
    f()



    1
    4
    9
    Thanks for some quick replies.

    I'm trying to minimize a function f(X) (X = [x1, x2, ..., xn]) using
    scipy.optimize.fmin_cobyla. The parameter of the function is a list of
    N variables. fmin_cobyla also accept a 'cons' parameter as constraint
    function, which is a list of functions.

    In order to make sure all variables be larger than 0, I tried to created
    constraint function like this:

    cons = []
    for i in range(N):
    c = lambda x: x[i] - 1e-10
    cons.append(c)

    But when functions in cons are evaluated, the value in all functions are
    all the same(N-1, which is the last value of i in the loop).

    What I want is, when functions in cons be evaluated, every function
    refers to a different variable in X.

    Regards

    Xiao Jianfeng
  • Duncan Booth at Jul 16, 2007 at 2:03 pm

    "fdu.xiaojf at gmail.com" wrote:

    In order to make sure all variables be larger than 0, I tried to created
    constraint function like this:

    cons = []
    for i in range(N):
    c = lambda x: x[i] - 1e-10
    cons.append(c)

    But when functions in cons are evaluated, the value in all functions are
    all the same(N-1, which is the last value of i in the loop).

    What I want is, when functions in cons be evaluated, every function
    refers to a different variable in X.
    So pass i in as a default value:

    for i in range(N):
    def c(x, i=i):
    return x[i] - 1e-10
    cons.append(c)
  • Fdu Xiaojf at Jul 16, 2007 at 3:04 pm

    Duncan Booth wrote:
    "fdu.xiaojf at gmail.com" wrote:
    In order to make sure all variables be larger than 0, I tried to created
    constraint function like this:

    cons = []
    for i in range(N):
    c = lambda x: x[i] - 1e-10
    cons.append(c)

    But when functions in cons are evaluated, the value in all functions are
    all the same(N-1, which is the last value of i in the loop).

    What I want is, when functions in cons be evaluated, every function
    refers to a different variable in X.
    So pass i in as a default value:

    for i in range(N):
    def c(x, i=i):
    return x[i] - 1e-10
    cons.append(c)
    Thanks all of you have read or replied.

    Both Duncan's and Steven's solution work well. Thanks!

    I love python, not only because of the language itself, and also the so kind
    community!


    Xiao Jianfeng
  • Wildemar Wildenburger at Jul 16, 2007 at 12:16 pm

    fdu.xiaojf at gmail.com wrote:
    I want to create a list of function.

    Here is my code:
    In [9]: a = []

    In [10]: for i in range(4):
    ....: b = lambda : i**2
    ....: a.append(b)
    ....:
    ....:

    In [11]: for f in a:
    ....: f()
    ....:
    ....:
    9
    9
    9
    9

    What I want is, the value of i should be bounded to the anonymous function.
    And the output should like this:

    for f in a:
    print f()

    1
    4
    9

    How to achieve this?

    Thanks a lot!
    I fail to see the point, sorry. Why would you want that? If you simply
    want a list of values you do:
    a = [i**2 for i in range(4)]
    Can you elaborarate on what it is you really want.

    /W
  • Wildemar Wildenburger at Jul 16, 2007 at 12:31 pm

    Wildemar Wildenburger wrote:
    for f in a:
    print f()

    1
    4
    9

    How to achieve this?

    Thanks a lot!

    I fail to see the point, sorry. Why would you want that? If you simply
    want a list of values you do:

    a = [i**2 for i in range(4)]
    Can you elaborarate on what it is you really want.

    /W
    Sorry, I'm an idiot. Nevermind. :-/

    /W
  • Bruno Desthuilliers at Jul 16, 2007 at 12:20 pm

    fdu.xiaojf at gmail.com a ?crit :
    I want to create a list of function.

    Here is my code:
    In [9]: a = []

    In [10]: for i in range(4):
    ....: b = lambda : i**2
    ....: a.append(b)
    ....:
    ....:

    In [11]: for f in a:
    ....: f()
    ....:
    ....:
    9
    9
    9
    9

    What I want is, the value of i should be bounded to the anonymous
    function. And the output should like this:

    for f in a:
    print f()

    1
    4
    9

    How to achieve this?
    a = [lambda i=i:i**2 for i in range(4)]
    for f in a:
    ... print f()
    ...

    1
    4
    9
    >>>

    HTH
  • Attn Steven Kuo at Jul 16, 2007 at 2:15 pm
    On Jul 16, 4:49 am, "fdu.xia... at gmail.com" wrote:

    (snipped)
    What I want is, the value of i should be bounded to the anonymous function.
    And the output should like this:

    for f in a:
    print f()

    1
    4
    9

    How to achieve this?

    Thanks a lot!


    The functools module, in Python 2.5 allows:
    import functools
    square = lambda i: i ** 2
    anon = [functools.partial(square, i) for i in range(4)]
    for f in anon:
    ... print f()
    ...

    --
    Hope this helps,
    Steven
  • Jeremy Sanders at Jul 16, 2007 at 3:14 pm

    fdu.xiaojf at gmail.com wrote:

    What I want is, the value of i should be bounded to the anonymous
    function. And the output should like this: ...
    How to achieve this?
    This doesn't answer your question (others have), but another (perhaps
    clearer) way to do such things is something like

    class MyFunc(object):
    """A function object."""
    def __init__(self, val):
    self.val = val

    def __call__(self):
    """Return value squared"""
    return self.val**2

    a = []
    for i in range(4):
    a.append(MyFunc(i))

    for f in a:
    f()


    Jeremy
  • Bruno Desthuilliers at Jul 15, 2007 at 10:42 pm

    Jeremy Sanders a ?crit :
    fdu.xiaojf at gmail.com wrote:

    What I want is, the value of i should be bounded to the anonymous
    function. And the output should like this: ...
    How to achieve this?

    This doesn't answer your question (others have), but another (perhaps
    clearer) way to do such things is something like

    class MyFunc(object):
    """A function object."""
    def __init__(self, val):
    self.val = val

    def __call__(self):
    """Return value squared"""
    return self.val**2

    a = []
    for i in range(4):
    a.append(MyFunc(i))

    for f in a:
    f()
    I wouldn't say it's "clearer" - at least in this concrete use case. And
    it takes more than just defining the __call__ method to have a fully
    function-like object (hint: try using a MyFunc instance as a method).

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 15, '07 at 10:42p
activeJul 16, '07 at 3:14p
posts11
users7
websitepython.org

People

Translate

site design / logo © 2022 Grokbase