0

What is the purpose of subclassing an instance of a class in Python? Here's an example:

class A:
    def __init__(*args): print(args) 

base = A() 


class Test(base): pass 

This code works properly under Python, but base is an instance of class A (1) Why do we need to subclass an instance of a class? Is it related to metaclasses?

From this question: What happens when you inherent from a module instead of a class in Python?

I understand that Test(base) will become type(base).__init__, (2) does this happen at definition time, when the class is defined? (3) How does Python know/decide that base is an instance of a class? Is it becuase type(base) doesn't return type?

Community
  • 1
  • 1
direprobs
  • 3,601
  • 18
  • 38

1 Answers1

1

Python actually uses type(base)(classname, bases, body) to produce the class object. This is the normal metaclass invocation (unless the class specifies a specific metaclass directly).

For an instance (or a module, which is basically an instance too), that means the class __new__ static method is called to produce a new instance, and on that new instance __init__ is called. So for your class Test(base): pass syntax, you are essentially creating a new instance of A, when Python executes the class statement.

This isn't really 'definition time'; there is no such phase in Python code. Python loads the bytecode for a module and executes it when it is imported for the first time. Class statements at the top of the module (so not inside functions) are executed at that time. It is at that time then that the type(base)(...) call is executed, yes.

Python doesn't 'know' or 'decide' anything about the base class. Python's philosophy is to trust the developer that writes code in it. The assumption is that you know what you are doing, and the base classes are just treated as if they'll respond correctly. Since type(base)(....) didn't raise an exception, Python just continued.

You can use anything that's callable as a metaclass, really.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997