V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
bulay
V2EX  ›  Python

类的实例通过 isinstance 判断时却返回 False

  •  
  •   bulay · 2020-07-31 10:58:05 +08:00 · 2063 次点击
    这是一个创建于 1358 天前的主题,其中的信息可能已经有所发展或是发生改变。

    环境: python-3.6.5 djangorestframework-3.10.3 django-2.2.3

    这是我自定义的一些异常: avatar

    继承关系是:WechatPaymentError -> BaseWechatError -> Exception 但是在统一处理异常时却出现了这样的情况: avatar

    通过 type(exc)可以看到 exc 是 WechatPaymentError 的实例, 但是对 WechatPaymentError 和 BaseWechatError 的 isinstance 判断都是 False

    第 1 条附言  ·  2020-07-31 17:53:48 +08:00
    更新:
    已经解决了,是命名空间的问题
    5 条回复    2020-07-31 17:43:53 +08:00
    SjwNo1
        1
    SjwNo1  
       2020-07-31 13:32:29 +08:00
    为什么在我的 shell 里没有这个问题
    zachlhb
        2
    zachlhb  
       2020-07-31 13:55:24 +08:00 via Android
    为何要判断类型,直接获取 exception 的 message 统一返回就行了啊
    chenstack
        3
    chenstack  
       2020-07-31 17:05:48 +08:00
    看下是否 type(exc) == WechatPaymentError,会不会定义了两个 WechatPaymentError
    Vegetable
        4
    Vegetable  
       2020-07-31 17:12:26 +08:00
    无法复现,检查命名空间是不是有问题吧,一般这种情况,都是代码出了问题。
    j0hnj
        5
    j0hnj  
       2020-07-31 17:43:53 +08:00
    如楼上所说,这种一般都是在两个模块里定义了两个 `WechatPaymentError`
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2598 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:39 · PVG 09:39 · LAX 18:39 · JFK 21:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.