Python论坛  - 讨论区

标题:[python-chinese] 回复: Python中有没有类似于PHP中function_exists的函数

2007年07月17日 星期二 17:35

汪文进 wjawon在yahoo.com.cn
星期二 七月 17 17:35:55 HKT 2007

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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月17日 星期二 18:23

Alex Cheng chengxin1985在gmail.com
星期二 七月 17 18:23:12 HKT 2007

ÓÃ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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号