python - 函数是 python 中的一流对象吗?

标签 python python-2.7 object

我正在学习 python 教程。它解释了函数如何成为 Python 中的一流对象。

def foo():
        pass
print(foo.__class__)

print(issubclass(foo.__class__,object))

我得到的上述代码的输出是

<type 'function'>
True

这个程序应该证明函数是 python 中的第一类对象?我的问题如下。

  1. 上面的代码如何证明函数是第一类对象?
  2. 一等对象的属性是什么?
  3. 什么是function.__class__表示?它返回一个元组 <type,function>哪个意义不大?

最佳答案

以下是 Guido 在他的博客中对一流对象的评价:

One of my goals for Python was to make it so that all objects were "first class." By this, I meant that I wanted all objects that could be named in the language (e.g., integers, strings, functions, classes, modules, methods, etc.) to have equal status. That is, they can be assigned to variables, placed in lists, stored in dictionaries, passed as arguments, and so forth.

whole blog post值得一读。

在您发布的示例中,教程可能指出一流对象通常是“对象”类的后代。

关于python - 函数是 python 中的一流对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23037519/

相关文章:

python - 如何在 Pandas 中比较前一行并使用 group by 更改另一列的值?

python - 如何让 pyautogui click 在 mac 上运行?

python - 在 Scrapy 中本地运行所有的爬虫

javascript - 如何使用正则表达式中的 或 选项提取和捕获特定的多个单词。?

python - for 循环里面的 if 条件

python - 从 pdf 中提取表格

javascript - 使用对象对数组项进行分组将超过 2 个元素

python - 如何在 MySQL 数据库上保存 python 对象?

objective-c - 每次将对象添加到队列或堆栈时是否都需要创建对象的新实例? Objective-C

python - Django:在一个查询中汇总计数