0
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

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 1
    This question needs a cleanup (I'm not 100% on what you're asking), but if I understand correctly, you made an instance of a class `A`, but testing it with `isinstance` says it is not an instance of `A`? I'd suggest providing real code, but a common cause of this problem is if you're working in the interactive interpreter and repeatedly defining `A` and `reload`ing the associated module. When you create an instance, it will be based on the currently loaded definition of `A`, but reloading the module will create a new, unrelated definition of `A`. – ShadowRanger Aug 30 '18 at 04:53
  • How about `from m1 import A` in the module m2? – Kota Mori Aug 30 '18 at 05:03
  • thank you – ShadowRanger – Kota Mori – jianlong wang Aug 30 '18 at 07:53

1 Answers1

0

you need to explicitly import the class you are checking against to E.g.

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