[Python-de] abfrage: Wert == [None] ???
Bertram Scharpf
lists at bertram-scharpf.de
Die Nov 9 15:55:06 CET 2004
Hallo,
Am Dienstag, 09. Nov 2004, 14:39:51 +0100 schrieb Frank Immich:
> eine Frage zu einer Abfrage:
> Ich weise in einem dictionary einem Schluessel den
> Wert [None] zu. Nun möchte ich abfragen ob der Wert = [None].
> Das funktioniert aber irgendwie nicht...?
>
> print "c:",c
>
> if c == [None]:
> print "Fall1:", p
> if c == ["None"]:
> print "Fall2:", p
> if c == "[None]":
> print "Fall3:", p
> if c == None:
> print "Fall3:", p
> if types.NoneType in c:
> print "Fall4:", p
> if c[0] == types.NoneType:
> print "Fall5:", p
>
> ergbnis:
>
> c: [None]
`c' ist nicht "None", sondern eine Liste mit genau einem
Element, und das ist "None". Mache Dir klar, worin der
Unterschied besteht zwischen:
None
[]
[ None]
[ 0]
type( None)
Berechne auch:
len( [ None])
not None
not []
not [ None]
Schneller als der Vergleich mit `==' geht bei None übrigens
der mit `is'.
n = None
print n == None
print n is None
print id( n) == id( None)
print id( n), id( None)
Viel Spaß!
Gruß
Bertram
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de