动态生成函数

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

动态生成函数

cafeeee
有如下一个函数:

def s(param):
   # ...
   # ...

如果想根据某m=dict("a": 0, "b": 1, "c":None)动态生成如下几个函数,应该怎么做?
def a():
   return s(0)

def b():
   return s(1)

def c():
   return s(None)


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 动态生成函数

Jiahua Huang
不知道你说的 dict("a": 0, "b": 1, "c":None) 是什么玩意,
猜测你想说 dict(a=0, b=1, c=None) 或 {"a": 0, "b": 1, "c":None}

>>> m=dict(a=0, b=1, c=None)
>>> for k,v in m.items():
...     exec('def %s(): return s(%s)'%(k,v))
...

2008/1/15 cafeeee <[hidden email]>:
> 如果想根据某m=dict("a": 0, "b": 1, "c":None)动态生成如下几个函数,应该怎么做?
> def a():
>     return s(0)
>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 动态生成函数

YiSu Zhou
In reply to this post by cafeeee
for a,b in dict:
    exec "def %s():\n return %s"%(a,repr(b))

在08-1-15,cafeeee <[hidden email] > 写道:
有如下一个函数:

def s(param):
   # ...
   # ...

如果想根据某m=dict("a": 0, "b": 1, "c":None) 动态生成如下几个函数,应该怎么做?
def a():
   return s(0)

def b():
   return s(1)

def c():
   return s(None)


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 动态生成函数

cafeeee
In reply to this post by Jiahua Huang
对,就是这个m={"a": 0, "b": 1, "c":None}

用exec是可以的,可是看起来不那么舒服,有没有更优雅的方法?
是不是用闭包可以解决这个问题?


2008/1/15 Jiahua Huang <[hidden email]>:
不知道你说的 dict("a": 0, "b": 1, "c":None) 是什么玩意,
猜测你想说 dict(a=0, b=1, c=None) 或 {"a": 0, "b": 1, "c":None}

>>> m=dict(a=0, b=1, c=None)
>>> for k,v in m.items():
...     exec('def %s(): return s(%s)'%(k,v))
...

2008/1/15 cafeeee <[hidden email]>:
> 如果想根据某m=dict("a": 0, "b": 1, "c":None)动态生成如下几个函数,应该怎么做?
> def a():
>     return s(0)
>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 动态生成函数

shhgs
不要用eval, exec。那是下三褴的Perl的方法。Python有更优雅的解决方案。

----------------------
class DynaFunc(object) :
    def __init__(self, d) :
        self.d = d
    def __getattr__(self, val) :
        if not val in self.d :
            raise NotImplementedError
        else :
            return lambda : s(d[val])

----------------------

2008/1/14 cafeeee <[hidden email]>:

> 对,就是这个m={"a": 0, "b": 1, "c":None}
>
> 用exec是可以的,可是看起来不那么舒服,有没有更优雅的方法?
> 是不是用闭包可以解决这个问题?
>
>
> 2008/1/15 Jiahua Huang < [hidden email]>:
>
>
> > 不知道你说的 dict("a": 0, "b": 1, "c":None) 是什么玩意,
> > 猜测你想说 dict(a=0, b=1, c=None) 或 {"a": 0, "b": 1, "c":None}
> >
> >
> > >>> m=dict(a=0, b=1, c=None)
> > >>> for k,v in m.items():
> > ...     exec('def %s(): return s(%s)'%(k,v))
> > ...
> >
> > 2008/1/15 cafeeee <[hidden email]>:
> >
> > > 如果想根据某m=dict("a": 0, "b": 1, "c":None)动态生成如下几个函数,应该怎么做?
> > > def a():
> > >     return s(0)
> > >
> > _______________________________________________
> > python-chinese
> > Post: send [hidden email]
> > Subscribe: send subscribe to [hidden email]
> > Unsubscribe: send unsubscribe to  [hidden email]
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
> _______________________________________________
> python-chinese
> Post: send [hidden email]
> Subscribe: send subscribe to [hidden email]
> Unsubscribe: send unsubscribe to  [hidden email]
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 动态生成函数

Qiangning Hong
In reply to this post by cafeeee
2008/1/15 cafeeee <[hidden email]>:
> 对,就是这个m={"a": 0, "b": 1, "c":None}
>
> 用exec是可以的,可是看起来不那么舒服,有没有更优雅的方法?
> 是不是用闭包可以解决这个问题?

for k, v in m.iteritems():
    locals()[k] = lambda: s(v)

用闭包替换lambda应该也可以

--
Qiangning Hong
http://www.douban.com/people/hongqn/
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 动态生成函数

shhgs
In reply to this post by shhgs
更正一下,有一个typo

           return lambda : s(d[val])

应该是

           return lambda : s(self.d[val])


2008/1/14 shhgs <[hidden email]>:

> 不要用eval, exec。那是下三褴的Perl的方法。Python有更优雅的解决方案。
>
> ----------------------
> class DynaFunc(object) :
>     def __init__(self, d) :
>         self.d = d
>     def __getattr__(self, val) :
>         if not val in self.d :
>             raise NotImplementedError
>         else :
>             return lambda : s(d[val])
>
> ----------------------
>
> 2008/1/14 cafeeee <[hidden email]>:
>
> > 对,就是这个m={"a": 0, "b": 1, "c":None}
> >
> > 用exec是可以的,可是看起来不那么舒服,有没有更优雅的方法?
> > 是不是用闭包可以解决这个问题?
> >
> >
> > 2008/1/15 Jiahua Huang < [hidden email]>:
> >
> >
> > > 不知道你说的 dict("a": 0, "b": 1, "c":None) 是什么玩意,
> > > 猜测你想说 dict(a=0, b=1, c=None) 或 {"a": 0, "b": 1, "c":None}
> > >
> > >
> > > >>> m=dict(a=0, b=1, c=None)
> > > >>> for k,v in m.items():
> > > ...     exec('def %s(): return s(%s)'%(k,v))
> > > ...
> > >
> > > 2008/1/15 cafeeee <[hidden email]>:
> > >
> > > > 如果想根据某m=dict("a": 0, "b": 1, "c":None)动态生成如下几个函数,应该怎么做?
> > > > def a():
> > > >     return s(0)
> > > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send [hidden email]
> > > Subscribe: send subscribe to [hidden email]
> > > Unsubscribe: send unsubscribe to  [hidden email]
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send [hidden email]
> > Subscribe: send subscribe to [hidden email]
> > Unsubscribe: send unsubscribe to  [hidden email]
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 动态生成函数

Jiahua Huang
In reply to this post by shhgs
倒,

那么 locales() 版本、globals() 版本、 __dict__ 版本都可以弄出一堆来了

2008/1/15 shhgs <[hidden email]>:
> 不要用eval, exec。那是下三褴的Perl的方法。Python有更优雅的解决方案。
>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 动态生成函数

cafeeee
In reply to this post by Qiangning Hong
这种方法好像不行:

>>> def s(p): print p
...
>>> m={"a":0, "b":1, "c":None}
>>> for k,v in m.iteritems():
...     locals()[k] = lambda: s(v)
...
>>> a()
1
>>> b()
1
>>> c()
1
>>>


2008/1/15 Qiangning Hong <[hidden email]>:

for k, v in m.iteritems():
   locals()[k] = lambda: s(v)

用闭包替换lambda应该也可以


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 动态生成函数

Lich_Ray
In reply to this post by cafeeee
python 3.0 来后所有有下标的类型都可以直接使用函数调用语法访问,这个问题就不存在了。说实话,3.0 的大多数改变我都比较讨厌,除了这个还有lambda的增强、列表模式匹配。

在08-1-15,cafeeee <[hidden email]> 写道:
有如下一个函数:

def s(param):
   # ...
   # ...

如果想根据某m=dict("a": 0, "b": 1, "c":None) 动态生成如下几个函数,应该怎么做?
def a():
   return s(0)

def b():
   return s(1)

def c():
   return s(None)


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese



--
Ray Stinger, nickname Lich_Ray
God is in his heaven, all's right with the world.
-------------------------------------------------
let focus = 'computing' in where:
http://lichray.javaeye.com
let focus = 'computing' in here:
http://lichray.bokeland.com
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 动态生成函数

Qiangning Hong
In reply to this post by cafeeee
2008/1/15 cafeeee <[hidden email]>:
> 这种方法好像不行:

那试试这个:

from functools import partial

for k, v in m.items():
    locals()[k] = partial(s, v)

--
Qiangning Hong
http://www.douban.com/people/hongqn/
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 动态生成函数

cafeeee
用闭包可以解决这个问题:

>>> def s(param): print param
...
>>> m={"a":1, "b":2, "c": None}
>>> for k,v in m.items():
...     def ss(param=v): s(param)
...     locals()[k] = ss
...
>>> a()
1
>>> b()
2
>>> c()
None
>>>

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese