2010-04-29 11 views
12

Mam prosty skrypt Pythona 'first.py':Uruchom plik python - jaka funkcja jest główna?

#first.py 
def firstFunctionEver() : 
    print "hello" 

firstFunctionEver() 

chcę nazwać ten skrypt za pomocą: python first.py i mieć go nazwać firstFunctionEver(). Ale scenariusz jest brzydki - jaką funkcję mogę ustawić w rozmowie na firstFunctionEver() i uruchomić ją po wczytaniu skryptu?

+0

https://stackoverflow.com/questions/419163/what-does-if-name-main-do –

Odpowiedz

31
if __name__ == "__main__": 
    firstFunctionEver() 

Czytaj więcej na docs here.

9
if __name__ == '__main__': 
    firstFunctionEver() 
+0

Co to jest "__name__" i kiedy to jest '__main__'? Proszę wyjaśnić i podać odniesienia. – agf

Powiązane problemy