4

Look at the following code:

a = 3
type(a) == object
False
isinstance(a, object)
True

How is this to be explained?

5
  • 1
    Object is base class. Type is metaclass. Commented Jul 13, 2018 at 6:44
  • @Mad Physicist,deleted. Commented Jul 13, 2018 at 6:46
  • Object is base-class. int is a sub_class from object. You can check here.stackoverflow.com/questions/100003/… Commented Jul 13, 2018 at 6:47
  • a is of type int, and intis a subclass of object. When you're using isinstance(object, classinfo) method, you're taking subclasses into account whereas using type(object) method generally returns the same than : object.__class__ attribute. Commented Jul 13, 2018 at 6:57
  • Python's recommandation is to use isinstance(object, classinfo) method to test the type. Commented Jul 13, 2018 at 6:58

2 Answers 2

4

Everything is an object in Python, which includes ints, strings, functions and classes. Therefore, isinstance(a, object) will return you True . But 3 is actually an integer, which is a sub_class create from object. Therefore type(a) can equal to int only.

I can give you an example.

Suppose we have two classes, Sub is a sub_class of Base.

class Base:
    def __init__(self):
        self.kappa='kappa'
class Sub(Base):
    def __init__(self):
        super().__init__()

obj=Base()
int_rino=Sub()


print(isinstance(obj, Base))
print(isinstance(obj, Sub))
print(isinstance(int_rino, Base))
print(isinstance(int_rino, Sub))


print(type(int_rino) == Base)

The result will be:

True
False
True
True
False
Sign up to request clarification or add additional context in comments.

4 Comments

based on your terminology I'm guessing that you're confusing baseclass and metaclass
Much better. I feel like I wrote that exact line "everything is an object in python" earlier today with the exact same emphasis. I'd recommend removing the superfluous comma.
@ Mad Physicis, ok, sir.
My mistake. My phrasing was slightly different: stackoverflow.com/a/51317646/2988730 :)
3

This is a common construct in most object-oriented languages that support inheritance. When a child class (in your case int) inherits from a parent (in your case object), it is said to have an "is-a" relationship. That is, an int is a (or an) object.

This "is-a" relationship is what isinstance is checking. From the docs:

Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof.

There is a similar issubclass function to check the same relationship for a class instead of an instance of that class. In fact, in most cases, isinstance(x, y) == issubclass(type(x), y).

type returns the exact class that an object was constructed from. That means that checking type(3) == object is exactly equivalent to checking int == object. Hopefully you can see that that's unambiguously false.

On a related tangent, classes should always be the same reference within a given run of the interpreter, so you can use is instead of == for comparison. So type(3) is int will be true. That's how == is implemented for all the types you're ever likely to come across anyway.

5 Comments

Great answer, however do not oppose class and instance because classes are instance of Type
@Florian. I wouldn't say that I'm opposing instances and types. Of course you're right that all classes are an instance of their metaclass (which doesn't have to be type or even inherit from it). However, I feel that this is a level of pedantic detail OP does not need and possibly can't handle at the moment. Also, keep in mind that functions like issubclass and isinstance require a class or tuple of classes for their second argument. So classes, regardless of metaclass, do have a special purpose here.
I had already upvoted, and with the edit, it's perfectly clear and accurate :) I was not accurate, classes are instance of Type by default, but it's not necessarily true. (for example : in most API using metaclasses as factory pattern)
That gets me thinking now. I wonder if I can make a plain function into a metaclass by slapping on __init__ and __new__ attributes.
The answer was correct in not mentioning metaclasses, class objects, and Type. They're not relevant to the OP's question, which is a mere problem of an instance being a subclass of a given type vs being that specific type. Every person is a mammal (and an animal) by inheritance, but they're still humans.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.