2007年07月17日 星期二 17:35
function_name = 'say_%s'%'hello' if function_name in [method for method in dir(object) if callable(getattr(object, method))]: getattr(object, function_name) ¸Ä×Ô Dive Into Python ----- ÔʼÓʼþ ---- ·¢¼þÈË£º Xell Zhang <xellzhang在gmail.com> ÊÕ¼þÈË£º python-chinese在lists.python.cn ÒÑ·¢ËÍ£º 2007/7/17(Öܶþ), ÏÂÎç5:03:20 Ö÷Ì⣺ Re: [python-chinese] PythonÖÐÓÐûÓÐÀàËÆÓÚPHPÖÐfunction_existsµÄº¯Êý£¿ ÊÇÕâÑùµÄ£¬±ÈÈçÓû§ÊäÈëfunction_name = 'hell'£¬ÎÒÏëÅжÏÊÇ·ñÓк¯Êý½Ð×ösay_hello£¬ÔÚphpÖÐÎÒ¿ÉÒÔÓÃfunction_exists('say' + function_name)À´ÅжÏÕâ¸öº¯ÊýÊÇ·ñ´æÔÚ£¬Èç¹û´æÔÚ£¬ÎÒ¿ÉÒÔÓÃcall_user_func_array('say' + function_name)À´µ÷ÓÃÕâ¸öº¯Êý¡£ pythonÄÜ×öÀàËƵÄÊÂÇéÂ𣿠On 7/17/07, limodou <limodou在gmail.com> wrote: On 7/17/07, Xell Zhang <xellzhang在gmail.com> wrote: > ¾ÍÊÇÅжϽÐij¸öÃû×ֵĺ¯ÊýÊÇ·ñ´æÔÚ¡£ > > ÁíÍâ¾ÍÊÇÓÐûÓÐÀàËÆPHPÖÐcall_user_func_arrayµÄº¯Êý£¿ > ¿ÉÒÔ¿¼ÂÇʹÓÃtry/exceptÀ´±£»¤Ò»Ï£¬ÁíÍâÈç¹ûÊÇÀàµÄ·½·¨£¬¿ÉÒÔʹÓÃhasattr£¬Èç¹ûÊÇÈ«¾Ö·½·¨£¬¿ÉÒÔÔÚfunc in globals()½øÐÐÅжϡ£ -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou _______________________________________________ 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 -- Zhang Xiao Engineer, Web development Ethos Tech. http://www.ethos.com.cn _______________________________________________ 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 ___________________________________________________________ ÇÀ×¢ÑÅ»¢Ãâ·ÑÓÊÏä3.5GÈÝÁ¿£¬20M¸½¼þ£¡ http://cn.mail.yahoo.com -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070717/fb6fac17/attachment-0001.htm
2007年07月17日 星期二 18:23
ÓÃgetattr() ÊÔÊÔ ´úÂë¿ÉÒÔÕâÑù£º def trycall(obj, name): funcName = "say " + name if obj.getattr(funcName, None) is not None: func = obj.getattr(funcName) if func.getattr("__call__", None) is not None: func() # call the function ... ... ÓжÎʱ¼äûд python µÄ³ÌÐòÁË£¬¿ÉÄÜ»áÓÐЩС´íÎó¡££º£© ÔÚ07-7-17£¬ÍôÎĽø <wjawon在yahoo.com.cn> дµÀ£º > > function_name = 'say_%s'%'hello' > > if function_name in [method for method in dir(object) if > callable(getattr(object, method))]: > getattr(object, function_name) > ¸Ä×Ô Dive Into Python > > ----- ÔʼÓʼþ ---- > ·¢¼þÈË£º Xell Zhang <xellzhang在gmail.com> > ÊÕ¼þÈË£º python-chinese在lists.python.cn > ÒÑ·¢ËÍ£º 2007/7/17(Öܶþ), ÏÂÎç5:03:20 > Ö÷Ì⣺ Re: [python-chinese] PythonÖÐÓÐûÓÐÀàËÆÓÚPHPÖÐfunction_existsµÄº¯Êý£¿ > > ÊÇÕâÑùµÄ£¬±ÈÈçÓû§ÊäÈëfunction_name = > 'hell'£¬ÎÒÏëÅжÏÊÇ·ñÓк¯Êý½Ð×ösay_hello£¬ÔÚphpÖÐÎÒ¿ÉÒÔÓÃfunction_exists('say' + > function_name)À´ÅжÏÕâ¸öº¯ÊýÊÇ·ñ´æÔÚ£¬Èç¹û´æÔÚ£¬ÎÒ¿ÉÒÔÓÃcall_user_func_array('say' + > function_name)À´µ÷ÓÃÕâ¸öº¯Êý¡£ > > pythonÄÜ×öÀàËƵÄÊÂÇéÂ𣿠> > On 7/17/07, limodou <limodou在gmail.com> wrote: > > > > On 7/17/07, Xell Zhang <xellzhang在gmail.com> wrote: > > > ¾ÍÊÇÅжϽÐij¸öÃû×ֵĺ¯ÊýÊÇ·ñ´æÔÚ¡£ > > > > > > ÁíÍâ¾ÍÊÇÓÐûÓÐÀàËÆPHPÖÐcall_user_func_arrayµÄº¯Êý£¿ > > > > > ¿ÉÒÔ¿¼ÂÇʹÓÃtry/exceptÀ´±£»¤Ò»Ï£¬ÁíÍâÈç¹ûÊÇÀàµÄ·½·¨£¬¿ÉÒÔʹÓÃhasattr£¬Èç¹ûÊÇÈ«¾Ö·½·¨£¬¿ÉÒÔÔÚfunc in > > globals()½øÐÐÅжϡ£ > > > > -- > > I like python! > > UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad > > My Blog: http://www.donews.net/limodou > > _______________________________________________ > > 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 > > > > > -- > Zhang Xiao > > Engineer, Web development > > Ethos Tech. > http://www.ethos.com.cn _______________________________________________ > 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 > > > ------------------------------ > ÑÅ»¢Ãâ·ÑÓÊÏä3.5GÈÝÁ¿£¬20M¸½¼þ£¡ <http://cn.mail.yahoo.com/> > > _______________________________________________ > 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 > -- powered by python -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070717/fa2f97fe/attachment-0001.htm
Zeuux © 2025
京ICP备05028076号