Python论坛  - 讨论区

标题:[python-chinese] [小技巧]Python函数的异常提示

2006年11月20日 星期一 16:02

刘鑫 march.liu在gmail.com
星期一 十一月 20 16:02:27 HKT 2006

ÓÐЩ´ÓJavaת¹ýÀ´µÄÅóÓÑ£¬¿ÉÄÜ»á±È½Ï»³ÄîJavaµÄÒì³£¶¨Ò壬Ҳ¾ÍÊÇ˵£¬¿ÉÒÔÔÚ¶¨ÒåÒ»¸öº¯Êýʱ£¬ÉùÃ÷Ëü¿ÉÄÜÅ׳öµÄÒì³£¡£ÒòΪÎÒ¼¸ºõûÓÐJavaÓïÑÔµÄʹÓþ­Ñ飨Èç¹û˵»á°²×°tomcatºÍEclipse¾ÍËãJava¸ßÊֵĻ°£¬ÄÇÎÒÊÇ£ºP£©£¬Òâʶ²»µ½ÕâÖÖ·½Ê½µÄÓŵ㡣Ôø¾­ÓкܾúöÂÔÁËËü¡£µ«ÊÇÔÚ¶Á¹ýһЩPythonÊé¼®ºó£¬ÎÒ·¢ÏÖ£¬ÕâÖÖÒì³£ÉùÃ÷¶ÔPythonÒ²ÊǺÜÓÐÒâÒåµÄ¡£ÓÚÊÇ£¬ÎÒÊÔ×ÅдÁËÒ»¸ödecorator
À´ÊµÏÖÕâ¸ö¹¦ÄÜ¡£ÊÂʵ֤Ã÷£¬PythonÀïËäȻûÓÐÓï·¨¼¶±ðµÄº¯ÊýÒì³£ÉùÃ÷£¬µ«ÊÇʵÏÖÕâÑùµÄ¹¦Äܷdz£¼òµ¥¡£ÒÔÏÂÊÇÒ»¸ö¼òµ¥µÄʾÀý£º

#-*- coding:utf-8 -*-

class ExpCollFunction(object):
    def __init__(self, fun, *exps):
        self.exps = tuple(exps)
        self.fun = fun
    def __call__(self, *args):
        return self.fun(*args)

class ExpInclude(object):
    def __init__(self, *exps):
        self.exps = []
        self.exps.extend(exps)
    def __call__(self, fun):
        return ExpCollFunction(fun, *self.exps)

@ExpInclude(Exception)
def foo():
    print "foo function"
    raise Exception()


ExpCollFunctionºÍExpIncludeÀàÐÍÊÇͨÓõģ¬¶¨Òå³ÉÒ»¸ö¶ÀÁ¢µÄÄ£¿é¾Í¿ÉÒÔʹÓá£Ö»ÒªÔÚ¶¨Òåijһ¸öº¯Êýʱ£¬ÏñʾÀýÖеÄfooÒ»Ñù£¬ÒÔExpIncludeÐÞÊΣ¬²¢ÔÚExpIncludeµÄ²ÎÊýÖд«ÈëÕâ¸öº¯Êý¿ÉÄÜÅ׳öµÄÒì³£ÀàÐͼ´¿É¡£³ýÁËdecorator
£¬ÕâÖÖ»¹¼¼ÇɵÃÒæÓÚPythonµÄÁíÍâÁ½¸öÓï·¨£¬Ò»¸öÊǶÔÏóµÄ¿Éµ÷ÓýӿÚ__call__£¬Ò»¸öÊÇexcept¿ÉÒÔ²¶»ñÒÔtupleÔ¼ÊøµÄÒ»×éÒì³£ÀàÐÍ¡£
ÀýÈ磬ÎÒÔÚidle shellÖÐÕâÑùÖ´ÐдúÂ룺
>>> try:
    foo()
except foo.exps:
    print "foo exception"


foo function
foo exception

ÁíÍ⣬һ¸ö²¶»ñ×Ô¶¨Òì³£µÄ¼òµ¥·½·¨ÊÇ£¬½«×Ô¶¨ÒåÒ쳣ͨ¹ý¶à¼Ì³ÐµÄ·½Ê½½øÐзÖÀ࣬ÕâÑù£¬²¶»ñʱ¿ÉÒÔ¼òµ¥µÄ½Ø»ñijһÖÖÒì³£µÄ×ÓÀ࣬¾ÍÏñGMailÀïµÄÓʼþ±êÇ©¡£ÕâÖÖ·½Ê½¿ÉÒԲμûBjarne
StroustrupµÄ¡¶C++ÓïÑÔµÄÉè¼ÆÓëÑÝ»¯¡·¡£
ÎÞÂÛÔÚPython»¹ÊÇJavaÖУ¬Ê¹ÓÃÒì³£ÉùÃ÷£¬ÒªÑϸñÈ·ÈÏÉùÃ÷µÄÒì³£ÁбíÓ뺯ÊýÖпÉÄÜÅ׳öµÄÒì³£ÕýÈ·¶ÔÓ¦£¬·ñÔòÖ»»áÊʵÃÆä·´¡£

-- 
»¶Ó­·ÃÎÊ£º
http://blog.csdn.net/ccat

ÁõöÎ
March.Liu
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061120/b52e7650/attachment-0001.html 

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

2006年11月20日 星期一 16:08

anfu niupichong niupichong.anfu在gmail.com
星期一 十一月 20 16:08:10 HKT 2006

²»´í,ÓÐÊÕ»ñ!лл.

2006/11/20, ÁõöÎ <march.liu在gmail.com>:
>
> ÓÐЩ´ÓJavaת¹ýÀ´µÄÅóÓÑ£¬¿ÉÄÜ»á±È½Ï»³ÄîJavaµÄÒì³£¶¨Ò壬Ҳ¾ÍÊÇ˵£¬¿ÉÒÔÔÚ¶¨ÒåÒ»¸öº¯Êýʱ£¬ÉùÃ÷Ëü¿ÉÄÜÅ׳öµÄÒì³£¡£ÒòΪÎÒ¼¸ºõûÓÐJavaÓïÑÔµÄʹÓþ­Ñ飨Èç¹û˵»á°²×°tomcatºÍEclipse¾ÍËãJava¸ßÊֵĻ°£¬ÄÇÎÒÊÇ£ºP£©£¬Òâʶ²»µ½ÕâÖÖ·½Ê½µÄÓŵ㡣Ôø¾­ÓкܾúöÂÔÁËËü¡£µ«ÊÇÔÚ¶Á¹ýһЩPythonÊé¼®ºó£¬ÎÒ·¢ÏÖ£¬ÕâÖÖÒì³£ÉùÃ÷¶ÔPythonÒ²ÊǺÜÓÐÒâÒåµÄ¡£ÓÚÊÇ£¬ÎÒÊÔ×ÅдÁËÒ»¸ödecorator
> À´ÊµÏÖÕâ¸ö¹¦ÄÜ¡£ÊÂʵ֤Ã÷£¬PythonÀïËäȻûÓÐÓï·¨¼¶±ðµÄº¯ÊýÒì³£ÉùÃ÷£¬µ«ÊÇʵÏÖÕâÑùµÄ¹¦Äܷdz£¼òµ¥¡£ÒÔÏÂÊÇÒ»¸ö¼òµ¥µÄʾÀý£º
>
> #-*- coding:utf-8 -*-
>
> class ExpCollFunction(object):
>     def __init__(self, fun, *exps):
>         self.exps = tuple(exps)
>         self.fun = fun
>     def __call__(self, *args):
>         return self.fun (*args)
>
> class ExpInclude(object):
>     def __init__(self, *exps):
>         self.exps = []
>         self.exps.extend(exps)
>     def __call__(self, fun):
>         return ExpCollFunction(fun, *self.exps)
>
> @ExpInclude(Exception)
> def foo():
>     print "foo function"
>     raise Exception()
>
>
> ExpCollFunctionºÍExpIncludeÀàÐÍÊÇͨÓõģ¬¶¨Òå³ÉÒ»¸ö¶ÀÁ¢µÄÄ£¿é¾Í¿ÉÒÔʹÓá£Ö»ÒªÔÚ¶¨Òåijһ¸öº¯Êýʱ£¬ÏñʾÀýÖеÄfooÒ»Ñù£¬ÒÔExpIncludeÐÞÊΣ¬²¢ÔÚExpIncludeµÄ²ÎÊýÖд«ÈëÕâ¸öº¯Êý¿ÉÄÜÅ׳öµÄÒì³£ÀàÐͼ´¿É¡£³ýÁËdecorator
> £¬ÕâÖÖ»¹¼¼ÇɵÃÒæÓÚPythonµÄÁíÍâÁ½¸öÓï·¨£¬Ò»¸öÊǶÔÏóµÄ¿Éµ÷ÓýӿÚ__call__£¬Ò»¸öÊÇexcept¿ÉÒÔ²¶»ñÒÔtupleÔ¼ÊøµÄÒ»×éÒì³£ÀàÐÍ¡£
> ÀýÈ磬ÎÒÔÚidle shellÖÐÕâÑùÖ´ÐдúÂ룺
> >>> try:
>     foo()
> except foo.exps:
>     print "foo exception"
>
>
> foo function
> foo exception
>
> ÁíÍ⣬һ¸ö²¶»ñ×Ô¶¨Òì³£µÄ¼òµ¥·½·¨ÊÇ£¬½«×Ô¶¨ÒåÒ쳣ͨ¹ý¶à¼Ì³ÐµÄ·½Ê½½øÐзÖÀ࣬ÕâÑù£¬²¶»ñʱ¿ÉÒÔ¼òµ¥µÄ½Ø»ñijһÖÖÒì³£µÄ×ÓÀ࣬¾ÍÏñGMailÀïµÄÓʼþ±êÇ©¡£ÕâÖÖ·½Ê½¿ÉÒԲμûBjarne
> StroustrupµÄ¡¶C++ÓïÑÔµÄÉè¼ÆÓëÑÝ»¯¡·¡£
> ÎÞÂÛÔÚPython»¹ÊÇJavaÖУ¬Ê¹ÓÃÒì³£ÉùÃ÷£¬ÒªÑϸñÈ·ÈÏÉùÃ÷µÄÒì³£ÁбíÓ뺯ÊýÖпÉÄÜÅ׳öµÄÒì³£ÕýÈ·¶ÔÓ¦£¬·ñÔòÖ»»áÊʵÃÆä·´¡£
>
> --
> »¶Ó­·ÃÎÊ£º
> http://blog.csdn.net/ccat
>
> ÁõöÎ
> March.Liu
>
> _______________________________________________
> 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/20061120/a9cd7b4e/attachment.htm 

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

2006年11月20日 星期一 18:27

yi huang yi.codeplayer在gmail.com
星期一 十一月 20 18:27:37 HKT 2006

def foo():
    print "foo function"
    raise Exception()
foo.exps = (Exception,)

# KISS

-- 
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20061120/974f1cf5/attachment.htm 

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

2006年11月20日 星期一 21:30

刘鑫 march.liu在gmail.com
星期一 十一月 20 21:30:52 HKT 2006

ÊÔÑéÁËһϣ¬ÕâÖÖ·½·¨ÊÇ¿ÉÐеģ¬ÏÔÈ»ÕâÑù¸ü¼ò¶Ì£¬Ò²ºÜÇåÎú¡£Èç¹ûÒ»¶¨ÒªÇ¿ÖÆÔ¼¶¨£¬Ò²¿ÉÒÔ°ÑÎÒµÄÕâ¸ö¹ý³Ì¼ò»¯Îª£º
class ExpInclude(object):
    def __init__(self, *exps):
        self.exps = tuple(exps)
    def __call__(self, fun):
        fun.exps = self.exps
        return fun

¶àлyi huang¡£

2006/11/20, yi huang <yi.codeplayer在gmail.com>:def foo():
    print "foo function"
    raise Exception()
foo.exps = (Exception,)

ÔÚ06-11-20£¬ÁõöÎ <march.liu在gmail.com> дµÀ£º
>
> ÓÐЩ´ÓJavaת¹ýÀ´µÄÅóÓÑ£¬¿ÉÄÜ»á±È½Ï»³ÄîJavaµÄÒì³£¶¨Ò壬Ҳ¾ÍÊÇ˵£¬¿ÉÒÔÔÚ¶¨ÒåÒ»¸öº¯Êýʱ£¬ÉùÃ÷Ëü¿ÉÄÜÅ׳öµÄÒì³£¡£ÒòΪÎÒ¼¸ºõûÓÐJavaÓïÑÔµÄʹÓþ­Ñ飨Èç¹û˵»á°²×°tomcatºÍEclipse¾ÍËãJava¸ßÊֵĻ°£¬ÄÇÎÒÊÇ£ºP£©£¬Òâʶ²»µ½ÕâÖÖ·½Ê½µÄÓŵ㡣Ôø¾­ÓкܾúöÂÔÁËËü¡£µ«ÊÇÔÚ¶Á¹ýһЩPythonÊé¼®ºó£¬ÎÒ·¢ÏÖ£¬ÕâÖÖÒì³£ÉùÃ÷¶ÔPythonÒ²ÊǺÜÓÐÒâÒåµÄ¡£ÓÚÊÇ£¬ÎÒÊÔ×ÅдÁËÒ»¸ödecorator
> À´ÊµÏÖÕâ¸ö¹¦ÄÜ¡£ÊÂʵ֤Ã÷£¬PythonÀïËäȻûÓÐÓï·¨¼¶±ðµÄº¯ÊýÒì³£ÉùÃ÷£¬µ«ÊÇʵÏÖÕâÑùµÄ¹¦Äܷdz£¼òµ¥¡£ÒÔÏÂÊÇÒ»¸ö¼òµ¥µÄʾÀý£º
>
> #-*- coding:utf-8 -*-
>
> class ExpCollFunction(object):
>     def __init__(self, fun, *exps):
>         self.exps = tuple(exps)
>         self.fun = fun
>     def __call__(self, *args):
>         return self.fun (*args)
>
> class ExpInclude(object):
>     def __init__(self, *exps):
>         self.exps = []
>         self.exps.extend(exps)
>     def __call__(self, fun):
>         return ExpCollFunction(fun, *self.exps)
>
> @ExpInclude(Exception)
> def foo():
>     print "foo function"
>     raise Exception()
>
>
> ExpCollFunctionºÍExpIncludeÀàÐÍÊÇͨÓõģ¬¶¨Òå³ÉÒ»¸ö¶ÀÁ¢µÄÄ£¿é¾Í¿ÉÒÔʹÓá£Ö»ÒªÔÚ¶¨Òåijһ¸öº¯Êýʱ£¬ÏñʾÀýÖеÄfooÒ»Ñù£¬ÒÔExpIncludeÐÞÊΣ¬²¢ÔÚExpIncludeµÄ²ÎÊýÖд«ÈëÕâ¸öº¯Êý¿ÉÄÜÅ׳öµÄÒì³£ÀàÐͼ´¿É¡£³ýÁËdecorator
> £¬ÕâÖÖ»¹¼¼ÇɵÃÒæÓÚPythonµÄÁíÍâÁ½¸öÓï·¨£¬Ò»¸öÊǶÔÏóµÄ¿Éµ÷ÓýӿÚ__call__£¬Ò»¸öÊÇexcept¿ÉÒÔ²¶»ñÒÔtupleÔ¼ÊøµÄÒ»×éÒì³£ÀàÐÍ¡£
> ÀýÈ磬ÎÒÔÚidle shellÖÐÕâÑùÖ´ÐдúÂ룺
> >>> try:
>     foo()
> except foo.exps:
>     print "foo exception"
>
>
> foo function
> foo exception
>
> ÁíÍ⣬һ¸ö²¶»ñ×Ô¶¨Òì³£µÄ¼òµ¥·½·¨ÊÇ£¬½«×Ô¶¨ÒåÒ쳣ͨ¹ý¶à¼Ì³ÐµÄ·½Ê½½øÐзÖÀ࣬ÕâÑù£¬²¶»ñʱ¿ÉÒÔ¼òµ¥µÄ½Ø»ñijһÖÖÒì³£µÄ×ÓÀ࣬¾ÍÏñGMailÀïµÄÓʼþ±êÇ©¡£ÕâÖÖ·½Ê½¿ÉÒԲμûBjarne
> StroustrupµÄ¡¶C++ÓïÑÔµÄÉè¼ÆÓëÑÝ»¯¡·¡£
> ÎÞÂÛÔÚPython»¹ÊÇJavaÖУ¬Ê¹ÓÃÒì³£ÉùÃ÷£¬ÒªÑϸñÈ·ÈÏÉùÃ÷µÄÒì³£ÁбíÓ뺯ÊýÖпÉÄÜÅ׳öµÄÒì³£ÕýÈ·¶ÔÓ¦£¬·ñÔòÖ»»áÊʵÃÆä·´¡£
>
> --
> »¶Ó­·ÃÎÊ£º
> http://blog.csdn.net/ccat
>
> ÁõöÎ
> March.Liu
>



-- 
»¶Ó­·ÃÎÊ£º
http://blog.csdn.net/ccat

ÁõöÎ
March.Liu
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061120/4a0c085d/attachment.html 

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号