Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “type”真的是函数吗?_Python_Function_Object_Python 3.x_Types - Fatal编程技术网

Python “type”真的是函数吗?

Python “type”真的是函数吗?,python,function,object,python-3.x,types,Python,Function,Object,Python 3.x,Types,首先,如果我问了一些愚蠢的问题,我很抱歉,因为我是Python新手… 我在读的时候看到了这句话: 函数的作用是:返回对象的类型(即对象本身) 当然,我决定检查一下: >>> def someFunction(x): ... return x * x ... >>> type(someFunction) <class 'function'> >>> type(type) <class 'type'>

首先,如果我问了一些愚蠢的问题,我很抱歉,因为我是Python新手…
我在读的时候看到了这句话:

函数的作用是:返回对象的类型(即对象本身)

当然,我决定检查一下:

>>> def someFunction(x):
...     return x * x
...     
>>> type(someFunction)
<class 'function'>
>>> type(type)
<class 'type'>
>>def someFunction(x):
...     返回x*x
...     
>>>类型(someFunction)
>>>类型(类型)

看起来函数有
函数
类型,但是如果函数是函数,为什么
类型
函数有不同的类型呢?或者文档是假的,它不是一个真正的函数?

是的,
type
是一个函数,但它是用C实现的

它也必须是它自己的类型,否则你不能:

>>> def foo(): pass
... 
>>> type(foo)
<type 'function'>
>>> type(type)
<type 'type'>
>>> isinstance(type(foo), type)
True
>>def foo():通过
... 
>>>类型(foo)
>>>类型(类型)
>>>iInstance(类型(foo),类型)
真的
e、 g.如果
类型
的类型不是
类型
,而是
函数
,则无法测试类型是否为类型。还有我吗

从技术上讲,
type
是一个可调用的类型,有两个相关的角色要扮演。它是一个元类(一个类工厂),是Python中所有类型的基础,调用它时会生成一个
类型
实例(
类型
类型的实例)


同样适用于所有类型(包括类别);调用它们,它们将生成给定类型的一个新实例。

type
是一个处理类对象的类。使用对象调用它只是使用它的一种方法。您还可以使用它来创建元类

比如说

>>> MyMetaClass = type("MyMetaClass", (object,), {'foo': 'bar'})
>>> newmeta = MyMetaClass()
>>> newmeta.foo
'bar'
>>> type(MyMetaClass)
<type 'type'>
>>> type(newmeta)
<class '__main__.MyMetaClass'>
MyMetaClass=type(“MyMetaClass”,(对象,),{'foo':'bar'}) >>>newmeta=MyMetaClass() >>>newmeta.foo “酒吧” >>>类型(MyMetaClass) >>>类型(newmeta)
  • 请注意,在Python中。对象可以是,即作为函数工作
  • type
    是一个对象,即可以作为函数调用的类型的对象。您可以使用一个参数调用它来获取对象的类型。但是还有另一个用例
  • type
    是一个,这意味着它本身就是一个类型,可以创建类(也可以是对象)
  • 所有类(以及元类)都是可调用的,这也是
    type
    是函数的另一个原因。您可以使用三个参数调用它来创建一个类
  • type
    是基本的内置元类。因此,它是Python中任何内容的基础。由于
    type
    位于类型的层次结构之上,
    type(type)
    返回
    type
    ,即
    type
    是其自身的类型
  • 你可能还想知道:


    这是一个用C实现的函数。它是它自己的类型。是的,是海龟,一路下来
    isinstance(类型,对象)
    isinstance(对象,类型)
    ,以及
    isinstance(类型,类型)
    都是
    True
    。显然……)另见。这是关于元类的,但它应该让您清楚地知道Python中
    type()
    的两个不同函数(类型检查和类工厂)的用途。我一直认为该类型对我来说是一个类,如果不能做到这一点就更好了。。。当测试另一种类型(不是
    type
    )时,有一种将第二个参数写入
    isinstance
    函数的通用方法?@SargeBorsch:为什么会这样?@SargeBorsch:
    isinstance(foo,types.FunctionType)
    工作得很好。不,其他人否定了这个答案;我很想知道为什么他们认为它值得一个。你的“元类”实际上不是一个元类-要成为元类,你必须有
    isinstance(newmeta,type)
    是真的。这意味着
    MyMetaClass
    是一个类,而不是元类。要使其成为元类,其实例必须是类,而不是类。它还需要是type的一个子类,而它不是。@ivc-我明白你现在说的,你是对的+1.