简体   繁体   English

如何在 None 的 Python 列表中设置意外类型

[英]How to set Unexpected Type in list of None's Python

I am trying to put a Port in a list of None's我正在尝试将端口放入 None 的列表中

ports = [None] * 5

for ident in range(len(self.ports)):
     if self.ports[ident] is None:
          try:
              serialC = Serial("COM" + str(ident), 19200)
              portX = Port(serialC, ident, self.main)
              #portX.start()
              self.ports[ident] = portX
          except Exception as e:
              print(e)
              continue

But I get an Unexpected type(s) (int, Port) Expected (int, None) (slice, itarable[None])但是我得到了一个意外的类型(int,端口)预期的(int,None)(slice,itarable [None])

With error message: Can't set attribute.带有错误消息:无法设置属性。

How do I fix this error?.我该如何解决这个错误?

class Port(threading.Thread):
    def __init__(self, port, ident, main):
        self.main = main
        self.port = port
        self.ident = ident
        threading.Thread.__init__(self)
Traceback (most recent call last):
  File "C:\Users\TDJ-PC\PycharmProjects\embeddedProject\serialTh.py", line 28, in check
    portX = Port(Serial("Com" + str(ident), 19200), ident, self.main)
  File "C:\Users\TDJ-PC\PycharmProjects\embeddedProject\port.py", line 10, in __init__
    self.ident = ident
AttributeError: can't set attribute

The ident property of a Thread object is not assignable; Thread object 的ident属性不可分配; you cannot set it from the __init__ method of your subclass, and it doesn't make sense to do so.您不能从子类的__init__方法中设置它,这样做没有意义。

From the documentation :文档中:

The 'thread identifier' of this thread or None if the thread has not been started.此线程的“线程标识符”,如果线程尚未启动,则为 None。 This is a nonzero integer.这是一个非零 integer。 See the get_ident() function.请参阅 get_ident() function。 Thread identifiers may be recycled when a thread exits and another thread is created.当一个线程退出并创建另一个线程时,线程标识符可能会被回收。 The identifier is available even after the thread has exited.即使在线程退出后,标识符仍然可用。

So, the ident property will be set for you automatically when you start the thread.因此,当您启动线程时,将自动为您设置ident属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM