Python论坛  - 讨论区

标题:[python-chinese] 怎样取module本身的object

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

zeng bobby bobby.zeng在gmail.com
星期二 七月 17 17:44:28 HKT 2007

hi£¡

pyÎļþÄÚÈçÏ£¬²»ÖªµÀ£¿ºÅ´¦Ð´Ê²Ã´£º
=========================================
import sys

def func1():
    pass

def func2():
    pass

for func in dir():
    object = ?
    getattr(object, func)

=========================================

TKS!
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070717/e73a9e70/attachment.html 

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

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

cafeeee cafeeee在gmail.com
星期二 七月 17 17:54:03 HKT 2007

ÊDz»ÊÇÓ¦¸ÃÓÃglobals£¿

for func in globals().values():
    if callable(func):
        ....



On 7/17/07, zeng bobby <bobby.zeng在gmail.com> wrote:
>
> hi£¡
>
> pyÎļþÄÚÈçÏ£¬²»ÖªµÀ£¿ºÅ´¦Ð´Ê²Ã´£º
> =========================================
> import sys
>
> def func1():
>     pass
>
> def func2():
>     pass
>
> for func in dir():
>     object = ?
>     getattr(object, func)
>
> =========================================
>
> TKS!
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070717/550d43a0/attachment.htm 

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

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

zeng bobby bobby.zeng在gmail.com
星期二 七月 17 18:02:24 HKT 2007

ллcafeeee

ÕÒµ½Ò»·½·¨£º
for funcname in locals().keys():
    ...
    func = locals()[funcname]

µ«²»ÖªµÀÓÃgatattrÄܲ»ÄÜ×öµ½¡£

ÔÚ07-7-17£¬cafeeee <cafeeee在gmail.com> дµÀ£º
>
> ÊDz»ÊÇÓ¦¸ÃÓÃglobals£¿
>
> for func in globals().values():
>     if callable(func):
>         ....
>
>
>
> On 7/17/07, zeng bobby < bobby.zeng在gmail.com> wrote:
> >
> > hi£¡
> >
> > pyÎļþÄÚÈçÏ£¬²»ÖªµÀ£¿ºÅ´¦Ð´Ê²Ã´£º
> > =========================================
> > import sys
> >
> > def func1():
> >     pass
> >
> > def func2():
> >     pass
> >
> > for func in dir():
> >     object = ?
> >     getattr(object, func)
> >
> > =========================================
> >
> > TKS!
> >
>
>
> _______________________________________________
> 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/20070717/66897c8b/attachment.html 

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

2007年07月17日 星期二 20:56

沈崴 wileishn在gmail.com
星期二 七月 17 20:56:50 HKT 2007

getattr 也可以:

$ echo -e 'def func():\n   print"hello
world!"\n\ngetattr(__import__("sys").modules["test"], "func")()' >
test.py && python -c "import test"

hello world!

在 07-7-17,zeng bobby<bobby.zeng在gmail.com> 写道:
> 谢谢cafeeee
>
> 找到一方法:
> for funcname in locals().keys():
>     ...
>     func = locals()[funcname]
>
> 但不知道用gatattr能不能做到。
>
> 在07-7-17,cafeeee < cafeeee在gmail.com> 写道:
> >
> > 是不是应该用globals?
> >
> > for func in globals().values():
> >     if callable(func):
> >         ....
> >
> >
> >
> >
> >
> > On 7/17/07, zeng bobby < bobby.zeng在gmail.com> wrote:
> > > hi!
> > >
> > > py文件内如下,不知道?号处写什么:
> > > =========================================
> > > import sys
> > >
> > > def func1():
> > >     pass
> > >
> > > def func2():
> > >     pass
> > >
> > > for func in dir():
> > >     object = ?
> > >     getattr(object, func)
> > >
> > > =========================================
> > >
> > > TKS!
> > >
> >
> >
> > _______________________________________________
> > 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
>

---
沈崴 (努力学习 Python 中 ...)
http://blog.163.com/eishn

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号