2

I met some problem with pyserial (v3.4) readlines function.

when I run some test codes in IDLE of python 3.5

>>> import serial
>>> ser = serial.Serial('com4', timeout=1)
>>> a = ser.readlines()
>>> a
[]

but when I use the compare "is []", it give me a False

>>> a is []
False

and when I use the compare "== []", it give me a True

>>> a == []
True

I don't understand why that can happen. Is there any way to fix this, or not?

If you could, please help me with this problem.

Thank you very much.


Thank for Jim Fasarakis Hilliard, I have known how to edit the code and make it beautiful in PIP8. I edit and add new code for someone who meet the same problem with me

>>> import serial
>>> ser = serial.Serial('com4', timeout=1)
>>> a = ser.readlines()
>>> if not a:
        DO SOMETHING WHAT YOU WANT
harn2412
  • 21
  • 3
  • thank you for your fast rely, but I cannot accept your answer. – harn2412 Sep 12 '17 at 16:53
  • Because readlines return the empty list [], but why "[] is []" get a False? I use pycharm IDE and it give me notice about this compare (PIP8). And I really want to know how to solve this problem? – harn2412 Sep 12 '17 at 17:00
  • @Jim Fasarakis Hilliard: "Why does “[] is [ ]” evaluate to False in python" I think this is what I need to know :D thank you very much. I cannot vote for you but really thank you again – harn2412 Sep 12 '17 at 17:30

0 Answers0