0
class Meta(type):
    def store(self):
        print("Meta store")


class Handler(metaclass=Meta):
    pass

For example, we have 2 above classes.

As you know, Meta inherit type for metaclass.

handler = Handler # without initialize
handler.store()

In this case, we can access Meta's store() method without initializing class. (handler = Handler)

How can it happen in metaclass?

Is there any documentation detailing the inner workings of metaclasses?

Thanks.

Hide
  • 3,199
  • 7
  • 41
  • 83
  • 1
    You *did* instantiate `Meta`. `Handler` is the instance you created. – user2357112 Jul 05 '22 at 05:30
  • @user2357112 Where do I instantiate Meta? Can you provide more detail? – Hide Jul 05 '22 at 05:31
  • classes in Python are objects, and metaclasses are just classes of classes. See https://stackoverflow.com/questions/100003/what-are-metaclasses-in-python – nigh_anxiety Jul 05 '22 at 05:35
  • 1
    A class is an instance of its metaclass, so when you declare `Handler`, you are creating an instance of `Meta` named `Handler`. – mipadi Jul 05 '22 at 05:35

0 Answers0