Identità vs Uguaglianza
Scopri la differenza tra identità e uguaglianza in Python. L'identità verifica se due variabili si riferiscono allo stesso oggetto in memoria, mentre l'uguaglianza confronta se due oggetti possiedono lo stesso valore. Comprendere questa distinzione è fondamentale per scrivere condizionali, validazioni e logiche di elaborazione dati accurate.
Che cos'è is?
isverifica l'identità dell'oggetto, ovvero se due variabili puntano allo stesso oggetto in memoria;==verifica l'uguaglianza dei valori, ovvero se due oggetti hanno gli stessi contenuti.
12345678# Comparing two shopping carts in an online store cart_today = ["milk", "bread"] cart_yesterday = ["milk", "bread"] shared_cart = cart_today print(cart_today == cart_yesterday) # True → same items print(cart_today is cart_yesterday) # False → two separate cart objects print(cart_today is shared_cart) # True → both refer to the same cart
Quando usare is e quando evitarlo
- Utilizzare
isper il confronto con singleton comeNone;
12345# Checking if the user has entered their phone number user_phone = None if user_phone is None: print("No phone number provided yet")
is notè il controllo di identità negato;
12345# Checking if the user's age is stored in the system user_age = 64 if user_age is not None: print(f"User age is recorded: {user_age}")
- Evitare l'uso di
isper verificare l'uguaglianza tra numeri o stringhe. A causa del caching/interning interno, l'identità può sembrare "funzionare" a volte, ma non è affidabile tra diverse esecuzioni e ambienti; utilizzare invece==;
123456789101112# Comparing user IDs and usernames in a system user_id_a = 256 user_id_b = 256 print(user_id_a == user_id_b) # True → same user ID value print(user_id_a is user_id_b) # May appear True, but identity check is unreliable for numbers username_a = "hello" username_b = "he" + "llo" print(username_a == username_b) # True → same text print(username_a is username_b) # Avoid using 'is' for string comparison (implementation detail)
- Per i booleani, preferire i controlli di veridicità.
12345# Checking if dark mode is enabled in user settings dark_mode_enabled = True if dark_mode_enabled: # clearer than: if dark_mode_enabled is True print("Dark mode is ON")
1. Completa gli spazi con is oppure ==:
2. Qual è il modo corretto per verificare l'assenza di "valore"?
3. Quale affermazione è raccomandata?
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Can you explain more about when to use `is` versus `==` in real-world scenarios?
What are some common mistakes people make with `is` and `==` in Python?
Could you provide more examples of identity and equality checks?
Awesome!
Completion rate improved to 3.45
Identità vs Uguaglianza
Scorri per mostrare il menu
Scopri la differenza tra identità e uguaglianza in Python. L'identità verifica se due variabili si riferiscono allo stesso oggetto in memoria, mentre l'uguaglianza confronta se due oggetti possiedono lo stesso valore. Comprendere questa distinzione è fondamentale per scrivere condizionali, validazioni e logiche di elaborazione dati accurate.
Che cos'è is?
isverifica l'identità dell'oggetto, ovvero se due variabili puntano allo stesso oggetto in memoria;==verifica l'uguaglianza dei valori, ovvero se due oggetti hanno gli stessi contenuti.
12345678# Comparing two shopping carts in an online store cart_today = ["milk", "bread"] cart_yesterday = ["milk", "bread"] shared_cart = cart_today print(cart_today == cart_yesterday) # True → same items print(cart_today is cart_yesterday) # False → two separate cart objects print(cart_today is shared_cart) # True → both refer to the same cart
Quando usare is e quando evitarlo
- Utilizzare
isper il confronto con singleton comeNone;
12345# Checking if the user has entered their phone number user_phone = None if user_phone is None: print("No phone number provided yet")
is notè il controllo di identità negato;
12345# Checking if the user's age is stored in the system user_age = 64 if user_age is not None: print(f"User age is recorded: {user_age}")
- Evitare l'uso di
isper verificare l'uguaglianza tra numeri o stringhe. A causa del caching/interning interno, l'identità può sembrare "funzionare" a volte, ma non è affidabile tra diverse esecuzioni e ambienti; utilizzare invece==;
123456789101112# Comparing user IDs and usernames in a system user_id_a = 256 user_id_b = 256 print(user_id_a == user_id_b) # True → same user ID value print(user_id_a is user_id_b) # May appear True, but identity check is unreliable for numbers username_a = "hello" username_b = "he" + "llo" print(username_a == username_b) # True → same text print(username_a is username_b) # Avoid using 'is' for string comparison (implementation detail)
- Per i booleani, preferire i controlli di veridicità.
12345# Checking if dark mode is enabled in user settings dark_mode_enabled = True if dark_mode_enabled: # clearer than: if dark_mode_enabled is True print("Dark mode is ON")
1. Completa gli spazi con is oppure ==:
2. Qual è il modo corretto per verificare l'assenza di "valore"?
3. Quale affermazione è raccomandata?
Grazie per i tuoi commenti!