Python id ()

Die Funktion id () gibt die Identität (eindeutige Ganzzahl) eines Objekts zurück.

Die Syntax von id()lautet:

 id (Objekt)

id () Parameter

id() Funktion nimmt ein einzelnes Parameterobjekt.

Rückgabewert von id ()

id()Funktion gibt die Identität des Objekts zurück. Dies ist eine Ganzzahl, die für das angegebene Objekt eindeutig ist und während seiner Lebensdauer konstant bleibt.

Beispiel 1: Wie funktioniert id ()?

 class Foo: b = 5 dummyFoo = Foo() print('id of dummyFoo =',id(dummyFoo))

Ausgabe

 ID von dummyFoo = 140343867415240

Weitere Beispiele zu id ()

 print('id of 5 =',id(5)) a = 5 print('id of a =',id(a)) b = a print('id of b =',id(b)) c = 5.0 print('id of c =',id(c))

Ausgabe

 ID von 5 = 140472391630016 ID von a = 140472391630016 ID von b = 140472391630016 ID von c = 140472372786520

Es ist wichtig zu beachten, dass alles in Python ein Objekt ist, gerade Zahlen und Klassen.

Daher hat die Ganzzahl 5eine eindeutige ID. Die ID der Ganzzahl 5bleibt während der Lebensdauer konstant. Ähnliches gilt für Float 5.5und andere Objekte.

Interessante Beiträge...