-
Öffnen Sie den VS Code-Editor in der LabEx-Umgebung.
-
Erstellen Sie eine neue Datei mit dem Namen hasattr_example.py
im Verzeichnis ~/project
.
touch ~/project/hasattr_example.py
-
Öffnen Sie die Datei hasattr_example.py
im Editor und fügen Sie den folgenden Code hinzu:
import math
## Check if the 'sqrt' function exists in the 'math' module
if hasattr(math, 'sqrt'):
print("The 'sqrt' function exists in the 'math' module.")
result = math.sqrt(25)
print("The square root of 25 is:", result)
else:
print("The 'sqrt' function does not exist in the 'math' module.")
## Check if the 'pi' constant exists in the 'math' module
if hasattr(math, 'pi'):
print("The 'pi' constant exists in the 'math' module.")
print("The value of pi is:", math.pi)
else:
print("The 'pi' constant does not exist in the 'math' module.")
## Check for a non-existent attribute
if hasattr(math, 'non_existent_attribute'):
print("The 'non_existent_attribute' exists in the 'math' module.")
else:
print("The 'non_existent_attribute' does not exist in the 'math' module.")
In diesem Code importieren wir zunächst das math
-Modul. Dann verwenden wir hasattr()
, um zu prüfen, ob die sqrt
-Funktion und die pi
-Konstante im math
-Modul existieren. Wir prüfen auch auf ein nicht existierendes Attribut, um zu sehen, wie hasattr()
diesen Fall behandelt.
-
Führen Sie das Skript mit dem folgenden Befehl im Terminal aus:
python ~/project/hasattr_example.py
Sie sollten die folgende Ausgabe sehen:
The 'sqrt' function exists in the 'math' module.
The square root of 25 is: 5.0
The 'pi' constant exists in the 'math' module.
The value of pi is: 3.141592653589793
The 'non_existent_attribute' does not exist in the 'math' module.
Diese Ausgabe zeigt, wie hasattr()
verwendet werden kann, um die Existenz von Funktionen und Konstanten innerhalb eines Moduls zu prüfen.
-
Versuchen wir nun, hasattr()
mit einem benutzerdefinierten Objekt zu verwenden. Ändern Sie das Skript wie folgt:
class MyClass:
def __init__(self):
self.attribute1 = "Hello"
def my_method(self):
return "World"
obj = MyClass()
## Check if the object has the attribute 'attribute1'
if hasattr(obj, 'attribute1'):
print("The object has the attribute 'attribute1'.")
print("The value of attribute1 is:", obj.attribute1)
else:
print("The object does not have the attribute 'attribute1'.")
## Check if the object has the method 'my_method'
if hasattr(obj, 'my_method'):
print("The object has the method 'my_method'.")
print("The result of my_method is:", obj.my_method())
else:
print("The object does not have the method 'my_method'.")
## Check for a non-existent attribute
if hasattr(obj, 'non_existent_attribute'):
print("The object has the attribute 'non_existent_attribute'.")
else:
print("The object does not have the attribute 'non_existent_attribute'.")
-
Führen Sie das Skript erneut aus:
python ~/project/hasattr_example.py
Sie sollten die folgende Ausgabe sehen:
The object has the attribute 'attribute1'.
The value of attribute1 is: Hello
The object has the method 'my_method'.
The result of my_method is: World
The object does not have the attribute 'non_existent_attribute'.
Dies zeigt, wie hasattr()
auch verwendet werden kann, um auf Attribute und Methoden in benutzerdefinierten Objekten zu prüfen.