参数化类和元类有什么区别(请使用 Python 代码示例)?

发布于 2024-09-14 13:18:16 字数 291 浏览 6 评论 0原文

大家好,Stack Overflow 贡献者,

我是一名正在学习 Python 的新手程序员,我发现了 这个网站有助于解释面向对象的范例。我知道元类是类的类(就像元目录是目录的目录等),但我遇到了一些麻烦:元类和参数化类之间的实际区别是什么,根据网站的定义?

如果可以的话,请提供 Python 代码示例来说明两者之间的差异。感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-09-21 13:18:16

Python 没有(或不需要)“参数化类”,因此很难在 Python 中提供它们的示例;-)。元类只是“类的类”:通常是 type (只要在 Py2 中,您记得通过继承 object 来使类成为新样式,或其他一些内置类型或其他新式类——旧式类是 Py2 中的遗留产物,幸运的是在 Py3 中消失了,理想情况下您应该忘记它们)。您可以出于多种高级目的创建自定义元类(通常是 type 的子类),但您不太可能需要(特别是考虑到这一点,从 python 2.6 开始,过去需要自定义元类的大部分工作现在可以使用类装饰器更简单地完成)。

给定任何类 C,type(C) 是它的元类。

参数化类是一个完全不同的概念。在 Python 中,最接近它的可能是一个工厂函数,它根据其参数创建并返回一个类:

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

再次强调,这绝对不是您经常需要的东西 - 制作几个不同的类只是通过一个或几个论点。不管怎样,正如你所看到的,它与类的类(又名元类)完全无关,在这个例子中它总是 type (并且在我能想到的几乎每个更现实的例子中 -我只是选择给出一个简单的例子,这样做的意义很难辨别,而不是一个现实的例子,因此必然非常复杂;-)。

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

白馒头 2024-09-21 13:18:16

这篇文章可能会有所帮助。 这个是一篇老文章,但也值得一读。我知道这并不能完全回答你的问题,但我希望它能给你带来思考。

This write up may be of help. And this one is an oldie but worth a read as well. I know that this doesn't fully answer your question but I hope it gives you food for thought.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文