ile birleştiğinde operatördür. Python 'abc'de' a 'ifadesi nasıl doğrudur? Ben şu iki ifadeleri değerlendirmek çalışıyordu:Python'un garip davranışı, 'in'
>>> 'a' in 'abc' is True:
False
>>> ('a' in 'abc') is True:
True
ile birleştiğinde operatördür. Python 'abc'de' a 'ifadesi nasıl doğrudur? Ben şu iki ifadeleri değerlendirmek çalışıyordu:Python'un garip davranışı, 'in'
>>> 'a' in 'abc' is True:
False
>>> ('a' in 'abc') is True:
True
İlginç bir soru (biliyorum anahtar kelime genellikle True
karşılaştırmak kullanılmamalıdır "is", bu sadece bir örnektir)! kabaca değerlendirir
>>> import dis
>>> dis.disassemble((lambda: ('a' in 'abc') is True).func_code)
1 0 LOAD_CONST 1 ('a') # stack: 'a'
3 LOAD_CONST 2 ('abc') # stack: 'a' 'abc'
6 COMPARE_OP 6 (in) # stack: True
9 LOAD_GLOBAL 0 (True)
12 COMPARE_OP 8 (is)
15 RETURN_VALUE
Yani ifade 'a' in 'abc' is True
gibi görünüyor: ('a' in 'abc') is True
elde edilen sonuç ile
>>> import dis
>>> dis.disassemble((lambda: 'a' in 'abc' is True).func_code)
2 0 LOAD_CONST 1 ('a') # stack: 'a'
3 LOAD_CONST 2 ('abc') # stack: 'a' 'abc'
6 DUP_TOP # stack: 'a' 'abc' 'abc'
7 ROT_THREE # stack: 'abc' 'a' 'abc'
8 COMPARE_OP 6 (in) # stack: 'abc' True
11 JUMP_IF_FALSE_OR_POP 21 # stack: 'abc'
14 LOAD_GLOBAL 0 (True) # stack: 'abc' True
17 COMPARE_OP 8 (is) # stack: False
20 RETURN_VALUE
>> 21 ROT_TWO
22 POP_TOP
23 RETURN_VALUE
Ve karşılaştırmak: Burada
'a' in 'abc' is True
gelen baytkodu var
>>> 'a' in 'abc' and 'abc' is True
O Bu, operatör zincirinin bir sonucu gibi görünüyor: https://stackoverflow.com/a/19751586/71522 - 1 < 5 < 10
'u doğru yapan aynı sihir.
Çok ilginç!
(Not: Bu, CPython 2.7.2 ile yapılmıştır)
Siz * operatörler * zincirleme *. –