Hello Stack Overflow contributers,
I'm a novice programmer learning Python right now, and I came upon this site which helps explain object-oriented paradigms. I know that metaclasses are classes of classes (like how meta-directories are directories of directories, etc. etc.), but I'm having trouble with something: What is the actual difference between a metaclass and a parameterized class, according to the website's definition?
If you can, please include code examples in Python that illustrate the differences between the two. Thank you for your help!
A Parameterized class is a Python class that inherits from param. Parameterized and can accept Parameter objects as class attributes. A Parameterized class or instance uses the Parameter objects to determine how the corresponding attribute should behave.
A metaclass in Python is a class of a class that defines how a class behaves. A class is itself an instance of a metaclass. A class in Python defines how the instance of the class will behave. In order to understand metaclasses well, one needs to have prior experience working with Python classes.
Just as an ordinary object is an instance of a class, any new-style class in Python, and thus any class in Python 3, is an instance of the type metaclass. In the above case: x is an instance of class Foo . Foo is an instance of the type metaclass.
__class__ is an attribute on the object that refers to the class from which the object was created. a. __class__ # Output: <class 'int'> b. __class__ # Output: <class 'float'> After simple data types, let's now understand the type function and __class__ attribute with the help of a user-defined class, Human .
Python doesn't have (or need) "parameterized classes", so it's hard to provide examples of them in Python;-). A metaclass is simply "the class of a class": normally type
(as long, in Py2, as you remember to make the class new-style by inheriting from object
, or some other built-in type or other new-style class -- old-style classes are a legacy artefact in Py2, fortunately disappeared in Py3, and you should ideally just forget about them). You can make a custom metaclass (usually subclassing type
) for several advanced purposes, but it's unlikely that you'll ever need to (esp. considering that, since python 2.6, much of what used to require a custom metaclass can now be done more simply with a class decorator).
Given any class C, type(C)
is its metaclass.
A parameterized class is a completely different concept. Closest you can come to it in Python is probably a factory function that makes and returns a class based on its arguments:
def silly(n):
class Silly(object):
buh = ' '.join(n * ['hello'])
return Silly
Silly1 = silly(1)
Silly2 = silly(2)
a = Silly1()
print(a.buh)
b = Silly2()
print(b.buh)
will print
hello
hello hello
Again, it's definitely not something you'll need often — making several classes that differ just by one or a few arguments. Anyway, as you can see, it has absolutely nothing to do with the classes' class (AKA metaclass), which is always type
in this example (and in almost every more realistic example I could think of — I just chose to give a simple example, where the point of doing this is hard to discern, rather than a realistic and therefore necessarily very complex one ;-).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With