Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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 为什么这个函数返回NoneType?_Python_Python 2.7_Types_User Defined Functions - Fatal编程技术网

Python 为什么这个函数返回NoneType?

Python 为什么这个函数返回NoneType?,python,python-2.7,types,user-defined-functions,Python,Python 2.7,Types,User Defined Functions,由于我在python方面没有那么丰富的经验,我知道的不多,但这让我感到困惑。这个函数似乎应该返回给定的类型,但是它不返回任何类型,我也不知道为什么 注意:我需要if-typeType部分,所以不要打高尔夫球了。因为typeint不是int;它的类型是: 但最好在此处使用映射: if Type is int: return 1 如果希望使用typesomething,则应传入int或str值: 而不是类型对象本身。将类型传递给函数,然后获取该类型的类型。因此,当您调用createint时

由于我在python方面没有那么丰富的经验,我知道的不多,但这让我感到困惑。这个函数似乎应该返回给定的类型,但是它不返回任何类型,我也不知道为什么

注意:我需要if-typeType部分,所以不要打高尔夫球了。

因为typeint不是int;它的类型是:

但最好在此处使用映射:

if Type is int:
    return 1
如果希望使用typesomething,则应传入int或str值:


而不是类型对象本身。

将类型传递给函数,然后获取该类型的类型。因此,当您调用createint时,函数将采用typeint,这是一种类型,与任何if语句都不匹配

>>> type(int)
<type 'type'>
if Type is int:
    return 1
return {int: 1, str: 'String'}[Type]
>>> type(1)
<type 'int'>
>>> type(1) is int
True