On 05/06/2014 10:28 AM, piergiorgio pancino wrote: > if __name__ == '__main__': > try: > csv_rapportino=ftp_connection.ftp_importazione() > db=SQLServer_connection.DB_connection().connect() > > ... > > except: > sys.exit() > > finally: > csv_rapportino.__exit__() > db.__exit__()
Dal (poco) codice che hai postato sembra che i due oggetti ftp_connection e SQLServer_connection vadano usati cosí: if __name__ == '__main__': with (ftp_connection.ftp_importazione() as csv_rapportino, SQLServer_connection.DB_connection().connect() as db): # resto del codice nel "try:" originale qui Questo perché: - lo statement "with" si occupa di chiamare __exit__() all'uscita dal blocco, indipendentemente dal fatto che si sia verificata un'eccezione nel mezzo o meno (https://docs.python.org/2/reference/compound_stmts.html#the-with-statement) - sempre "with" si occupa anche di chiamare __enter__() all'entrata del blocco (non conosoco le classi che stai usando, ma é anche possibile che la connessione venga effettuata lí..) - non nascondere le eccezioni quando non serve: quell'``except: sys.exit()`` é completamente inutile (in caso di eccezione non gestita, l'interprete sa che deve terminare l'esecuzione con codice di errore) peró maschera un traceback potenzialmente utile per capire cosa é andato storto.. -- Samuele ~redShadow~ Santi ---------------------------------------------------------------------- ----( Contact information )---- E-MAIL: redsha...@hackzine.org BLOG: http://www.hackzine.org WIKI: http://wiki.hackzine.org TWITTER: https://twitter.com/_rshk GITHUB: https://github.com/rshk ----( GPG Key: 09F09951 )---- 1AFC FAE5 CA4B 3FB0 F300 FA64 3CB0 1008 09F0 9951 ----( Registered Linux-User: #440008 )---- DEBIAN User since 2004 (I even used Sid!) GENTOO User since 2008-01-01 ---------------------------------------------------------------------- Think about the environment: for each top-posted, badly quoted or bulk e-mail I receive, I'm going to cut down a tree. _______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python