Remembering the context

Discussion in 'Python' started by GZ, Apr 28, 2010.

  1. GZ

    GZ Guest

    Hi All,

    I am looking at the following code:

    def fn():

    def inner(x):
    return tbl[x]

    tbl={1:'A', 2:'B'}
    f1 = inner # I want to make a frozen copy of the values of tbl
    in f1
    tbl={1:'C', 2:'D'}
    f2 = inner
    return (f1,f2)

    f1,f2 = fn()
    f1(1) # output C
    f2(1) # output C

    What I want is for f1 to make a frozen copy of tbl at the time f1 is
    made and f2 to make another frozen copy of tbl at the time f2 is made.
    In other words, I want f1(1)=='A' and f2(1)=='C'.

    One way to do this is to use functools.partial

    def fn():

    def inner(tbl, x):
    return tbl[x]

    tbl={1:'A', 2:'B'}
    f1 = functools.partial(inner,tbl) # I want to make a frozen copy
    of the values of tbl in f1
    tbl={1:'C', 2:'D'}
    f2 = functools.partial(inner,tbl)
    return (f1,f2)

    I am wondering if there is any other way to do this.
     
    GZ, Apr 28, 2010
    #1
    1. Advertising

  2. GZ

    Chris Rebert Guest

    On Wed, Apr 28, 2010 at 1:31 AM, GZ <> wrote:
    > I am looking at the following code:
    >
    > def fn():
    >
    >    def inner(x):
    >         return tbl[x]
    >
    >    tbl={1:'A', 2:'B'}
    >    f1 = inner   # I want to make a frozen copy of the values of tbl
    > in f1
    >    tbl={1:'C', 2:'D'}
    >    f2 = inner
    >   return (f1,f2)
    >
    > f1,f2 = fn()
    > f1(1)  # output C
    > f2(1) # output C
    >
    > What I want is for f1 to make a frozen copy of tbl at the time f1 is
    > made and f2 to make another frozen copy of tbl at the time f2 is made.
    > In other words, I want f1(1)=='A' and f2(1)=='C'.
    >
    > One way to do this is to use functools.partial
    >
    > def fn():
    >
    >    def inner(tbl, x):
    >         return tbl[x]
    >
    >    tbl={1:'A', 2:'B'}
    >    f1 = functools.partial(inner,tbl)   # I want to make a frozen copy
    > of the values of tbl in f1
    >    tbl={1:'C', 2:'D'}
    >    f2 = functools.partial(inner,tbl)
    >   return (f1,f2)
    >
    > I am wondering if there is any other way to do this.


    I prefer the functools.partial() method personally, but the other
    obvious way to go about it is:

    def fn():
    tbl={1:'A', 2:'B'}
    f1 = lambda x, table=tbl: table[x]

    tbl={1:'C', 2:'D'}
    f2 = lambda x, table=tbl: table[x] # we have to be repetitive

    return (f1,f2)

    Note that default argument values are evaluated exactly once, at
    definition-time.
    The lambdas can equivalently be replaced with def-s of course.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Apr 28, 2010
    #2
    1. Advertising

  3. GZ wrote:
    > Hi All,
    >
    > I am looking at the following code:
    >
    > def fn():
    >
    > def inner(x):
    > return tbl[x]
    >
    > tbl={1:'A', 2:'B'}
    > f1 = inner # I want to make a frozen copy of the values of tbl
    > in f1
    > tbl={1:'C', 2:'D'}
    > f2 = inner
    > return (f1,f2)
    >
    > f1,f2 = fn()
    > f1(1) # output C
    > f2(1) # output C
    >
    > What I want is for f1 to make a frozen copy of tbl at the time f1 is
    > made and f2 to make another frozen copy of tbl at the time f2 is made.
    > In other words, I want f1(1)=='A' and f2(1)=='C'.
    >


    something like

    def makeInner(a_tbl):
    def inner(x):
    return a_tbl[x]
    return inner

    def fn():
    tbl = {1:'A', 2:'B'}
    f1 = makeInner(tbl)
    tbl = {1:'C', 2:'D'}
    f2 = makeInner(tbl)
    return f1,f2

    f1,f2 = fn()

    In [4]: print f1(1)
    A

    In [5]: print f2(1)
    C

    JM

    PS : smelling code anyway :eek:)
     
    Jean-Michel Pichavant, Apr 29, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VS_NET_DEV
    Replies:
    2
    Views:
    3,807
    jenny
    May 25, 2004
  2. =?Utf-8?B?U3VuU21pbGU=?=
    Replies:
    0
    Views:
    709
    =?Utf-8?B?U3VuU21pbGU=?=
    Jan 10, 2006
  3. Flip Rayner
    Replies:
    1
    Views:
    669
    bruce barker
    Jan 23, 2007
  4. asd
    Replies:
    1
    Views:
    441
    www.pulpjava.com
    Nov 9, 2006
  5. Jani Tiainen
    Replies:
    3
    Views:
    1,028
    Jani Tiainen
    Aug 27, 2007
Loading...

Share This Page