In questa lezione svilupperemo alcuni esempi di classi in Python, al fine di consolidare quanto appreso nelle lezioni precedenti.
Classi in Python – primo esempio
Sviluppiamo dunque un primo esempio nel quale definiamo una classe Student:
class Student:
def __init__(self, name, lastname, age):
self.name = name
self.lastname = lastname
self.age = age
Salviamo questa classe nel file ‘Student.py‘ e poi in un altro file Python lo importiamo utilizzando from ed import.
Definiamo un’istanza di Student e poi la stampiamo:
from Student import *
student1 = Student("Nick", "Rossi", 23)
print("Name =", student1.name, "\nLastname =", student1.lastname, "\nAge =", student1.age)
In output avremo quindi:
Name = Nick
Lastname = Rossi
Age = 23
Stiamo associando alla variabile student1 un’istanza dell’oggetto Student.
Cosa succede se ad esempio omettiamo un qualsiasi parametro?
(Ricondizionato) TP-Link TL-WPA4220 KIT Powerline Kit AV600Mbps e WiFi 300Mbps, 2 Pezzi, Homeplug AV2, Porte Ethernet, Plug & Play, Tecnologia Powerline Avanzata, Espandi la Portata WiFi
39,99 € (a partire da 18 Marzo 2024 14:59 GMT +01:00 - Altre informazioniProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Microsoft Surface Book 2 13.5" - Core i5, 8 GB RAM, 256 GB (Clavier Britannico QWERTY) (ricondizionato)
356,89 € (a partire da 18 Marzo 2024 15:18 GMT +01:00 - Altre informazioniProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)(Ricondizionato) TP-Link M7000 Saponetta 4G LTE Cat4 150Mbps, WiFi N300Mbps, Router con SIM, Portatile, Batteria Ricaricabile, Configurazione Semplice, Red DOT Design Award
39,99 € (a partire da 18 Marzo 2024 14:59 GMT +01:00 - Altre informazioniProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Facciamo dunque un esempio dove omettiamo di inserire l’età:
from Student import *
student1 = Student("Nick", "Rossi")
print("Name =", student1.name, "\nLastname =", student1.lastname)
In questo caso il codice restituirà un errore:
...
TypeError: Student.init() missing 1 required positional argument: ‘age’
Questo perchè tutti i parametri sono obbligatori.
Come fare allora per rendere un parametro opzionale? Lo studieremo nel prossimo paragrafo.
Classi in Python – rendere un parametro opzionale
Per rendere un parametro opzionale, possiamo ad esempio assegnare un valore di default come ad esempio 0.
class Student:
def __init__(self, name, lastname, age = 0):
self.name = name
self.lastname = lastname
self.age = age
In questo modo quando definirò l’istanza student1 non si avrà più l’errore di prima:
from Student import *
student1 = Student("Nick", "Rossi")
print("Name =", student1.name, "\nLastname =", student1.lastname)
L’output sarà quindi:
Name = Nick
Lastname = Rossi
Possiamo sempre assegnare un valore ad age, in modo da sovrascrivere il valore di default:
from Student import *
student1 = Student("Nick", "Rossi", 22)
print("Name =", student1.name, "\nLastname =", student1.lastname, "\nAge =", student1.age)
Possiamo assegnare i valori di default ad uno o più parametri.
Facciamo dunque un altro esempio:
class Student:
def __init__(self, name = "Nick", lastname = "Rossi", age = 0):
self.name = name
self.lastname = lastname
self.age = age
Definiamo un’istanza senza passare alcun valore ma lasciando i valori di default:
student1 = Student()
print("Name =", student1.name, "\nLastname =", student1.lastname, "\nAge =", student1.age,)
Conclusioni
In questa lezione abbiamo studiato alcuni esempi di classi in Python, nelle prossime lezioni vedremo come utilizzare i metodi delle classi.
Alcuni link utili
Indice tutorial sul linguaggio Python
Operazioni con i file in Python
Introduzione ai dizionari in Python
Generico Karmatech Hard Disk Esterno, Disco Rigido Esterno Portatile 2.5, USB 3.0, 500 GB, Nero (500 GB) (Ricondizionato)
20,00 € (a partire da 18 Marzo 2024 14:59 GMT +01:00 - Altre informazioniProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)TECLAST F16Pro PC Portatile 15,6 Pollici FHD, 16GB RAM 512GB SSD, Intel N95 Notebook(fino a 3.4Ghz), Tastiera Retroilluminata Computer Portatile, WiFi 6/BT 5.0, HDMI, Type C, USB3.0, Laptop 2024
369,99 € (a partire da 18 Marzo 2024 14:37 GMT +01:00 - Altre informazioniProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)MSI Thin GF63 12UDX-293IT, Notebook Gaming, 15.6" FHD 144Hz, Intel i7-12650H, Nvidia RTX 3050 6GB GDDR6, RAM 16GB DDR4 3200MHz, 512B SSDPCIe 4, WiFi 6, Win 11 Home, Layout e Garanzia ITA, Nero
874,21 € (a partire da 18 Marzo 2024 14:37 GMT +01:00 - Altre informazioniProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Quali sono i metodi delle liste