Metaclass and is instance

Origin

Django uses the metaclass ModelBase for models (django.db.models.Model) to perform various registration processes.

So I was reading the source

class ModelBase(type):
    def __new__(cls, name, bases, attrs):
        parents = [b for b in bases if isinstance(b, ModelBase)]

There was a code like this, so when I typed ʻis instance (Question, Model) `[^ 1] with" Hmm "and shell,

False

[^ 1]: Question is a class that inherits Model. That is, Django's model class

If you think about it carefully, you can understand it, but at first I thought "that?", So I will summarize what I understood.

What is is instance

isinstance returns True if the first argument ** object ** is an instance of the second argument ** class ** .. For example

isinstance(123, int)

Returns True.

What went wrong with the previous input

Question is ** class **, Model is also ** class **. In other words, you will be asked, "Is the Question class an instance of the Model class?" No such thing. Not surprisingly, False is returned. Subclasses and instances were messed up.

Then how should I ask

Apart from isinstance, there is a function called issubclass. You can use this.

issublass(Question, Model)

True is returned safely.

Metaclass and is instance

Let's get back to the Django source. In the Django source, I was checking if the superclasses of "classes to be created" are instances of ModelBase [^ 2]. The first argument is ** class **. The reason for this is that the second argument is a ** metaclass ** that inherits type. ** Since the instance of the metaclass is a class **, this check is True if the class inherits the Model class. That is, the following expression returns True.

isinstance(Question, ModelBase)

[^ 2]: It seems that you can create an abstract model class that defines common attributes in the application, but is it practical to create such a complicated model hierarchy?

Recommended Posts

Metaclass and is instance
Python is instance
About Class and Instance
Python: Class and instance variables
What is an instance variable?
[Python] Python and security-① What is Python?
python metaclass and sqlalchemy declareative
Identity and equivalence Python is and ==
Use instance method and class method properly
Difference between == and is in python
Python classes and instances, instance methods
[Python] What is pandas Series and DataFrame?
Your ansible and serverspec combination is wrong!
AWS EC2 instance launch and ssh connection
Day 64 pip install tensorflow and 2.0 is here.
How to use is and == in Python