“类本身就是对象"是什么意思? [英] What is meant by "classes themselves are objects"?
问题描述
我只是在阅读有关类的Python文档;它说,在Python中,类本身就是对象".这与C#,Java,Ruby或Smalltalk中的类有何不同?与其他语言相比,这种类型的类有哪些优缺点?
I was just reading the Python documentation about classes; it says, in Python "classes themselves are objects". How is that different from classes in C#, Java, Ruby, or Smalltalk? What advantages and disadvantages does this type of classes have compared with those other languages?
推荐答案
在Python中,类是对象,从某种意义上讲,您可以像分配其他对象一样将它们分配给变量,将它们传递给函数等.例如
In Python, classes are objects in the sense that you can assign them to variables, pass them to functions, etc. just like any other objects. For example
>>> t = type(10)
>>> t
<type 'int'>
>>> len(t.__dict__)
55
>>> t() # construct an int
0
>>> t(10)
10
Java具有 Class
对象,这些对象提供有关类的一些信息,但是您不能使用它们代替显式的类名.它们不是真正的类,而只是类信息结构.
Java has Class
objects which provide some information about a class, but you can't use them in place of explicit class names. They aren't really classes, just class information structures.
Class C = x.getClass();
new C(); // won't work
这篇关于“类本身就是对象"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!