appunti3s:modulo_python
This is an old revision of the document!
Usare un Modulo
Per usare alcune funzioni bisogna fare import (in python3 diventa tkinter)
>>> import Tkinter >>> mario = Tkinter.Tk()
Quando si fa import Tkinter viene cercato un file Tkinter.py dentro qualche cartella (si veda sys.path)
Creare un modulo
Dato che un modulo da importare non è altro che un file.py, è facile crearne uno
miomodulo.py
class Pianeta: def __init__(self): self.name = 'pianeta' def funzione(x, y): return x + y def main(): pass # Any code you like if __name__ == '__main__': main()
Il test if __name__ permette di fare due cose:
- importare il modulo e usare le sue funzioni a richiesta
- importare il modulo ed eseguire direttamente il suo contenuto (main)
appunti3s/modulo_python.1593965453.txt.gz · Last modified: 2020/07/05 18:10 by profpro