python __main__ is similar to java main() 
Author Message
 python __main__ is similar to java main()

Well I've read the docs and searched in the newsgroup archives but
I'm still confused about __main__

        I  expected it to startup the program, like main in java but
when I feed it to the interpreter he just "eats" the file loads wathever
is in the file but doesn't run main.

        So am I suposed to explicitly call it like in the file below or is
there a better (pythonic) way to do it??

        TIA

        David Nunes

import string
import PedLan
import GLOBAL

class Lancamento:
    ReqID = 0

    #PDNConsulta

    def ___init__(self):
        #inicializacao do PDNConsulta
        pass

    def trataPedido(self):#nao faz verificacao de input
        linha = raw_input('>')
        parse = string.splitfields(linha, GLOBAL.INPUTSEP)
        disc = parse[0]
        nomes = []
        notas = []
        for indice in range(1,len(parse), 2):
            nomes.append(parse[indice])
        for indice in range(2,len(parse), 2):
            notas.append(parse[indice])
        pedido = PedLan.PedLan(ReqID, nomes, notas)
        #<PDNConsulta.Lancamento(pedido)

    def __main__(self):
        print('running')
        interface = Lancamento()
        while(1):
            interface.trataPedido()

Lancamento().__main__() #to run main ????



Mon, 06 Dec 2004 01:01:19 GMT  
 python __main__ is similar to java main()


Quote:
> Well I've read the docs and searched in the newsgroup archives but
> I'm still confused about __main__

>         I  expected it to startup the program, like main in java but
> when I feed it to the interpreter he just "eats" the file loads wathever
> is in the file but doesn't run main.

>         So am I suposed to explicitly call it like in the file below or is
> there a better (pythonic) way to do it??

the idiom is this:

class MyClass:
  pass

if __name__ == '__main__':
  # do stuff with MyClass

what is happening here is __name__ is defined to be the name of the module when
the file is called via 'import' or 'from' or it is called '__main__' if being
used as a script and called directly by the interpreter.  Note __name__ is at
the module scope, not part of any class.

This allows for each module to have test code in it that can be executed easily.

If you are simply writing a #!python style script there is no need for main,
the interpreter starts at the first line and executes everything it sees.



Mon, 06 Dec 2004 01:50:12 GMT  
 python __main__ is similar to java main()

Quote:

> class Lancamento:
>     ReqID = 0

>     #PDNConsulta

>     def ___init__(self):
>         #inicializacao do PDNConsulta
>         pass

Not answering the immediate question, but pointing out a bug:

  def __init__(self):

is the correct spelling.  If it was just a typo, you're okay,
but if you actually cut-and-pasted the text you've got a latent
problem in your code with the triple leading underscore.

-Peter



Mon, 06 Dec 2004 07:35:38 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. What does MAIN_ _MAIN_ __MAIN__ mean?(to invoke clapack)

2. ENTRY in main (was re: Jumping from nested subroutine to main directly)

3. if __name__=='__main__': main()

4. Hiding main TK main window

5. I am looking for a VI that login and logout users to a main VI.

6. where is my main method(like java)??

7. How to exit the main script in Python

8. Newbie question: embedding python in a c++ main

9. Proposal: Python main function

10. Preferred way to input python commands from the main loop

11. ATTACHX problem: task doesn't terminate after termination of main program

12. Best way to get MAIN pgmname (EXEC PGM=?)

 

 
Powered by phpBB® Forum Software