简体   繁体   English

Python中的类对象和类类型

[英]Class object and class type in Python

I am new to Python and I have a question about OOP.我是 Python 新手,我有一个关于 OOP 的问题。 So, when we create a class we write "object" in parentheses.因此,当我们创建一个类时,我们在括号中写上“对象”。 For example,例如,

class My(object):

I have read somewhere that this means that the class inherits from the object class in Python.我在某处读到这意味着该类继承自 Python 中的object类。 But I also have read that every class we create is an instance of the type class.但我也读过我们创建的每个类都是type类的实例。 So my question is the object class the same as the type class and, if yes, why, when ask Python to print the type of a class, it returns <class 'type'> and not <class 'object'> .所以我的问题是object类与type类相同,如果是,为什么当要求 Python 打印类的类型时,它返回<class 'type'>而不是<class 'object'> Also, why can't we write还有,为什么不能写

class My(type):

if the object class and the type class are the same?如果object类和type类相同?

There are two relationships here: instance of and subtype of .这里有两种关系: instance ofsubtype of For a non-programming analogy, you can think about the difference like this.对于非编程类比,您可以像这样考虑差异。 A particular apple sitting on a desk is an instance of "apple".坐在桌子上的特定苹果是“apple”的一个实例 It's an example of an apple, that particular one.这是一个苹果的例子,那个特别的。 On the other hand, "apple" as a concept is a subtype of "fruit".另一方面,“苹果”作为一个概念是“水果”的一个子类型 Every apple is a fruit.每个苹果都是水果。 It wouldn't be accurate to say that the particular apple sitting on your desk is a subtype of anything, because it's a specific one.说坐在你桌上的特定苹果是任何事物的子类型是不准确的,因为它是特定的。 And it wouldn't make sense to say the concept of "apple" is an instance of "fruit", because it's not.说“苹果”的概念是“水果”的一个实例是没有意义的,因为它不是。

Every class you define in Python is a subtype of object , which is the type of all types.你在 Python 中定义的每个类都是object子类型,它是所有类型的类型。 The class itself is also an object in Python, and its type is type .本身也是 Python 中的一个对象,它的类型是type The latter can be used to do some clever metaprogramming, but that's advanced Python that you probably don't need to be messing with at this stage.后者可用于进行一些巧妙的元编程,但这是您现阶段可能不需要搞乱的高级 Python。

class Foo(object):
  pass

assert isinstance(Foo, type) # Foo is a type
assert issubclass(Foo, object) # Foo is a subclass of object
assert isinstance(Foo, object) # Every type (and everything in Python) is an object
assert isinstance(Foo(), Foo) # A specific foo is a Foo
assert isinstance(Foo(), object) # A specific foo is an object

For right now, as you're learning Python, it's pretty safe to just forget type exists.现在,当你正在学习 Python 时,忘记type的存在是很安全的。 It's fancy and complicated and used for some really sophisticated metaprogramming tricks.它花哨而复杂,用于一些非常复杂的元编程技巧。 For now, just use object and assume classes are some magical thing, and learn how to use them.现在,只需使用object并假设类是一些神奇的东西,然后学习如何使用它们。 You can learn the gritty details of how they work once you have a firm foundation.一旦你有了坚实的基础,你就可以了解它们如何工作的细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM