2

I read that "A metaclass is the class of a class. Like a class defines how an instance of the class behaves, a metaclass defines how a class behaves. A class is an instance of a metaclass."

I was wondering how different this is from interface in Java. interface too provides a blueprint for the classes to follow.

Darshan Chaudhary
  • 2,093
  • 3
  • 23
  • 42
  • 1
    No, it is not. You might want to read this http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python. – adarsh Oct 13 '15 at 07:37
  • I read just that and got confused. I wished to see how `metaclass` is different from `interface`. They both define some rules which all the classes inheriting them must follow, right ? – Darshan Chaudhary Oct 13 '15 at 07:40
  • At a basic level metaclass is similar to the `Class` class in java... But again, Java != Python... So you cannot expect them to be exactly same... – Codebender Oct 13 '15 at 07:51
  • Abstract base classes are the similar concept to Java's interfaces. Metaclasses are totally different things. –  Jan 06 '19 at 09:30

1 Answers1

4

No.

About Python Metaclass:
Metaclass in Python is a class which is used to create classes. A class that you create in Python is simply an instance of its metaclass. type is the super most metaclass in Python.

In Python, a class is modeled as an object, which gives you a lot of flexibility in its usage, by attaching all the properties of objects to classes. For example, you can even create class definition dynamically, just the way you would create an instance of a class.
This is a good read.

About Interfaces in Java:
First of all, an interface is not a class. It is just another type (Both interfaces and classes are types in Java). It is true that classes and interfaces are closely related in Java (An interface is a blueprint of a class). But they are entirely different types. Interface in java is simply a mechanism to achieve abstraction.

Conclusion: In Python, you really don't have anything like java interface.

Jithin Pavithran
  • 1,250
  • 2
  • 16
  • 41