V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Jul 31, 2020 · 2733 views
    This topic created in 2102 days ago, the information mentioned may be changed or developed.

    环境: 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

    Supplement 1  ·  Jul 31, 2020
    更新:
    已经解决了,是命名空间的问题
    5 replies    2020-07-31 17:43:53 +08:00
    SjwNo1
        1
    SjwNo1  
       Jul 31, 2020
    为什么在我的 shell 里没有这个问题
    zachlhb
        2
    zachlhb  
       Jul 31, 2020 via Android
    为何要判断类型,直接获取 exception 的 message 统一返回就行了啊
    chenstack
        3
    chenstack  
       Jul 31, 2020
    看下是否 type(exc) == WechatPaymentError,会不会定义了两个 WechatPaymentError
    Vegetable
        4
    Vegetable  
       Jul 31, 2020
    无法复现,检查命名空间是不是有问题吧,一般这种情况,都是代码出了问题。
    j0hnj
        5
    j0hnj  
       Jul 31, 2020
    如楼上所说,这种一般都是在两个模块里定义了两个 `WechatPaymentError`
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2505 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 03:13 · PVG 11:13 · LAX 20:13 · JFK 23:13
    ♥ Do have faith in what you're doing.