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;-).