简体   繁体   中英

use isinstance() cannot check python object class import other module

class A:
    somemethod

class A in module m1

in module m2 I want to use isinstance() to check object obj1 is or not class A

and obj1 = A()

but isinstance(obj1,A) is 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM