[英]use isinstance() cannot check python object class import other module
class A:
somemethod
class A
in module m1
模块
m1
class A
in module m2
I want to use isinstance()
to check object obj1
is or not class A
在模块
m2
我想使用isinstance()
检查对象obj1
是否为A
类
and obj1 = A()
和
obj1 = A()
but isinstance(obj1,A)
is False
.... type(obj1) == <class, m1.A>
但
isinstance(obj1,A)
是False
.... type(obj1) == <class, m1.A>
I can't understand why?我不明白为什么? help me
帮我
you need to explicitly import the class you are checking against to Eg您需要明确地将您正在检查的类导入到 Eg
from bs4 import BeautifulSoup
...
print(type(td)) --> outputs {type} = <class 'bs4.element.Tag'>
if isinstance(td, Tag): --> {NameError}name 'Tag' is not defined
so you need to import Tag
from bs4.element import Tag
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.