繁体   English   中英

您可以像perl一样在python顶部调用main函数吗?

[英]Can you call the main function at the top in python like perl?

有谁知道有没有办法做到这一点?

例如,此perl代码有效,但是python无效吗?

#!/usr/bin/perl5.18
main();

sub main {
    print 'hello\n';
    return;
}
#!/usr/bin/env python3.4
main()

def main():
    print('hello')
    return

谢谢

您可以尝试:

def main():
    print('hello')

然后将其称为main()

在调用main之前,需要先编写main定义:

def main():
    print('hello')
    return

if __name__ == "__main__": # Avoid running main function when this file is imported instead of run directly.
    main()

在声明函数之前,不能调用该函数,除非该调用在另一个函数中。 解决方法:

def top():
    main()

...

def main():
    print('hello')
    return

top()

不知道为什么要这么做。 无论哪种语言。 在Perl中肯定很容易出错。

因此,您只能在更早定义它后才能使用该函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM