[英]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.