【问题标题】:Finding the types of function查找函数类型
【发布时间】:2020-07-13 13:21:10
【问题描述】:
def hello():
    x=1
    return
print(type(hello))

<class 'function'> 作为其输出,但是

def hello():
    x=1
    return
print(type(hello()))

<class 'NoneType'> 作为其输出。为什么会这样?

【问题讨论】:

  • 可能要指定语言

标签: python function oop


【解决方案1】:

您已经定义了一个名为hello 的函数,因此,当您打印变量hellotype 时,您会得到它的类型是函数:

print(type(hello))

输出:

<class 'function'>

但是,当您打印hello() 的类型时,您不是打印变量,而是打印函数执行后返回的类型。在您的定义中,hello 不返回任何内容,或 None,因此打印执行 hello 的结果类型的输出正确无:

print(type(hello())) 

输出:

<class 'None'>

【讨论】:

    【解决方案2】:

    这是因为hello它的函数类型变量

    typeof()函数获取变量

    当你在函数名后面加上() 时,你会播放函数并且它不再是可变的。

    typeof()函数不能将其作为变量引用,无法识别

    【讨论】:

      猜你喜欢
      • 2019-05-29
      • 2012-05-09
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 2016-04-14
      相关资源
      最近更新 更多