簡體   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