2008年01月15日 星期二 11:31
ÓÐÈçÏÂÒ»¸öº¯Êý£º
def s(param):
   # ...
   # ...
Èç¹ûÏë¸ù¾Ýijm=dict("a": 0, "b": 1, "c":None)¶¯Ì¬Éú³ÉÈçϼ¸¸öº¯Êý£¬Ó¦¸ÃÔõô×ö£¿
def a():
   return s(0)
def b():
   return s(1)
def c():
   return s(None)
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080115/7a620eb5/attachment.htm 
2008年01月15日 星期二 11:46
不知道你说的 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 <cafeeee at gmail.com>:
> 如果想根据某m=dict("a": 0, "b": 1, "c":None)动态生成如下几个函数,应该怎么做?
> def a():
>     return s(0)
>
2008年01月15日 星期二 11:51
for a,b in dict:
    exec "def %s():\n return %s"%(a,repr(b))
ÔÚ08-1-15£¬cafeeee <cafeeee在gmail.com> дµÀ£º
>
> ÓÐÈçÏÂÒ»¸öº¯Êý£º
>
> def s(param):
>    # ...
>    # ...
>
> Èç¹ûÏë¸ù¾Ýijm=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 python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080115/b23e3326/attachment.html 
2008年01月15日 星期二 12:02
¶Ô£¬¾ÍÊÇÕâ¸öm={"a": 0, "b": 1, "c":None}
ÓÃexecÊÇ¿ÉÒԵģ¬¿ÉÊÇ¿´ÆðÀ´²»ÄÇÃ´Êæ·þ£¬ÓÐûÓиüÓÅÑŵķ½·¨£¿
ÊDz»ÊÇÓñհü¿ÉÒÔ½â¾öÕâ¸öÎÊÌ⣿
2008/1/15 Jiahua Huang <jhuangjiahua在gmail.com>:
> ²»ÖªµÀÄã˵µÄ 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 <cafeeee在gmail.com>:
> > Èç¹ûÏë¸ù¾Ýijm=dict("a": 0, "b": 1, "c":None)¶¯Ì¬Éú³ÉÈçϼ¸¸öº¯Êý£¬Ó¦¸ÃÔõô×ö£¿
> > def a():
> >     return s(0)
> >
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080115/4525a82a/attachment.htm 
2008年01月15日 星期二 12:34
不要用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 <cafeeee在gmail.com>:
> 对,就是这个m={"a": 0, "b": 1, "c":None}
>
> 用exec是可以的,可是看起来不那么舒服,有没有更优雅的方法?
> 是不是用闭包可以解决这个问题?
>
>
> 2008/1/15 Jiahua Huang < jhuangjiahua在gmail.com>:
>
>
> > 不知道你说的 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 <cafeeee在gmail.com>:
> >
> > > 如果想根据某m=dict("a": 0, "b": 1, "c":None)动态生成如下几个函数,应该怎么做?
> > > def a():
> > >     return s(0)
> > >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
2008年01月15日 星期二 12:36
2008/1/15 cafeeee <cafeeee在gmail.com>: > 对,就是这个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/
2008年01月15日 星期二 12:36
更正一下,有一个typo
           return lambda : s(d[val])
应该是
           return lambda : s(self.d[val])
2008/1/14 shhgs <shhgs.efhilt在gmail.com>:
> 不要用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 <cafeeee在gmail.com>:
>
> > 对,就是这个m={"a": 0, "b": 1, "c":None}
> >
> > 用exec是可以的,可是看起来不那么舒服,有没有更优雅的方法?
> > 是不是用闭包可以解决这个问题?
> >
> >
> > 2008/1/15 Jiahua Huang < jhuangjiahua在gmail.com>:
> >
> >
> > > 不知道你说的 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 <cafeeee在gmail.com>:
> > >
> > > > 如果想根据某m=dict("a": 0, "b": 1, "c":None)动态生成如下几个函数,应该怎么做?
> > > > def a():
> > > >     return s(0)
> > > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
2008年01月15日 星期二 12:39
倒, 那么 locales() 版本、globals() 版本、 __dict__ 版本都可以弄出一堆来了 2008/1/15 shhgs <shhgs.efhilt at gmail.com>: > 不要用eval, exec。那是下三褴的Perl的方法。Python有更优雅的解决方案。 >
2008年01月15日 星期二 14:00
ÕâÖÖ·½·¨ºÃÏñ²»ÐУº
>>> 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 <hongqn在gmail.com>:
>
> for k, v in m.iteritems():
>    locals()[k] = lambda: s(v)
>
> ÓñհüÌæ»»lambdaÓ¦¸ÃÒ²¿ÉÒÔ
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080115/a9c48941/attachment.html 
2008年01月15日 星期二 18:44
python 3.0 À´ºóËùÓÐÓÐϱêµÄÀàÐͶ¼¿ÉÒÔÖ±½ÓʹÓú¯Êýµ÷ÓÃÓï·¨·ÃÎÊ£¬Õâ¸öÎÊÌâ¾Í²»´æÔÚÁË¡£ËµÊµ»°£¬3.0µÄ´ó¶àÊý¸Ä±äÎÒ¶¼±È½ÏÌÖÑᣬ³ýÁËÕâ¸ö»¹ÓÐlambdaµÄÔöÇ¿¡¢ÁбíģʽƥÅä¡£ ÔÚ08-1-15£¬cafeeee <cafeeee在gmail.com> дµÀ£º > > ÓÐÈçÏÂÒ»¸öº¯Êý£º > > def s(param): > # ... > # ... > > Èç¹ûÏë¸ù¾Ýijm=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 python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: 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 -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20080115/abbf0ffe/attachment.html
2008年01月15日 星期二 23:36
2008/1/15 cafeeee <cafeeee在gmail.com>: > 这种方法好像不行: 那试试这个: from functools import partial for k, v in m.items(): locals()[k] = partial(s, v) -- Qiangning Hong http://www.douban.com/people/hongqn/
2008年01月16日 星期三 15:30
Óñհü¿ÉÒÔ½â¾öÕâ¸öÎÊÌ⣺
>>> 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
>>>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080116/dcfbc864/attachment-0001.html 
Zeuux © 2025
京ICP备05028076号