Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:28:24+00:00 2026-06-18T04:28:24+00:00

From this post – What's the canonical way to check for type in Python?

  • 0

From this post – What's the canonical way to check for type in Python?, I could use this code to check object o is string type.

o = "str"; print type(o) is str --> True

However, with user defined type, type(a) is A doesn’t seem to work.

class A:
    def hello(self):
        print "A.hello"
a = A()

print type(a) is A # --> False
print type(a) == A # --> False

Why is this? How can I get the correct type checking for user defined type?
I use python 2.7 on Mac OS X.

PS: This is a question out of curiosity, as I got this example from this book to get true as a result, but I got false. I understand that Duck typing is a preferred way in python. (https://stackoverflow.com/a/154156/260127)

ADDED

rodrigo’s answer works for me. Using ‘isinstance’ doesn’t give me an exact type, it just tests if an object is an instance of a class or a subclass.

class C(A):
    def hello(self):
        print "C.hello"

a = A()
c = C()

print isinstance(a, A) --> True
print isinstance(c, A) --> True
print isinstance(a, C) --> False
print isinstance(c, C) --> True
print "----"
print type(a) == A --> True
print type(c) == A --> False

ADDED 2

jdurango’s answer (a.__class__ is A) gave me pretty interesting Java equivalent.

a.getClass() == A.class <--> a.__class__ == A (a.__class__ is A)
a isinstance A <--> isinstance(a, A)
c isinstance A <--> isinstance(c, A)

I don’t know which copied which.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:28:25+00:00Added an answer on June 18, 2026 at 4:28 am

    You should use the new-style classes:

    class A(object):
        pass
    

    That is, derive it from object.

    The problem is that object of old-style are implemented as if all of them were of type instance.

    Deriving directly or indirectly from object will solve this issue. Or moving to Python3, where there is no more old-style classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I took a method from this post (accepted answer code) that serializes object so
I want to use the code from this post in a base page that
I have learnt from this post that always use <a> tags or <button> tags
This is a separate question stemming from this post: How to use the filename
I understand from this post , that it's an anti-pattern to modify Object 's
I am trying out the code from this post on Event Driven Architecture (very
I have this code with the switch statement which I got from this post
I use tip from this post https://stackoverflow.com/a/13838907 to open new tab, however when I
I found this code on here from this post: jQuery Accordion: links don't work
I am executing following code from this post : preg_match_all(/'(.+)' ((\w+)\(? ?(\d*) ?\)?)/, $sql,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.