2007年07月17日 星期二 17:44
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
2007年07月17日 星期二 17:54
Ê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
2007年07月17日 星期二 18:02
лл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
2007年07月17日 星期二 20:56
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
Zeuux © 2025
京ICP备05028076号