Python函数原型

2024-04-26 12:39:28 发布

您现在位置:Python中文网/ 问答频道 /正文

def tempr():
        print("in the function")

.
.
.
.
tempr()

python函数就是这样工作的,而

tempr()
.
.
.
.
.
def tempr():
    print("in the function")

这行不通

while in c++ we have a prototype declaration to tell the compiler that the function exists. what do I do here? Can Python handle such kind of c++ kind of function call statement above the function definition


Tags: ofthe函数indefhavefunctiondo
2条回答

简单的回答是:您必须在代码中遵循top-down结构,这是一个很好的实践。设置它的方式基本上导致调用undefined函数,因为它甚至没有在内存堆栈中设置(在运行时)

what do I do here?

在调用函数之前定义或导入该函数。这是没有办法的

Can Python handle such kind of c++ kind of function call statement above the function definition

不,Python目前没有函数原型。正如Iain Shelvington所说,函数和变量都是对象。(从字面上说,Python中的一切都是对象)因为在使用之前必须定义一个变量,与函数相同。如果不想看到,可以将函数放入另一个文件并导入

相关问题 更多 >