Pycharm 从队列中放入和获取后丢失对象指示器

Pycharm lose object indicator after put and get from queue

我知道这不是主要问题。但是当我的对象有很多变量并且每个变量都很长时,它就变得很烦人。有什么方法可以让 Python/Pycharm 知道 curCat 类型吗?喜欢类型转换?编写创建新对象并复制每个变量的函数看起来没有必要...

from queue import Queue


class Cat:

    def __init__(self):
        self.name = 'cat'


q = Queue()
c = Cat()
q.put(c)

cur = q.get()

print(cur.name)    <= No 'name' indicator shows up

至少 python。你不能从 class 内部访问一个变量,而在它外部,除非你在你的情况下使用全局变量,你可以使用这样的东西。

from queue import Queue

class Cat:
     def __init__(self):
             self.name = 'cat'

// Find variable
global name
// Your code can go here this can just be a check to see if it has functioned correctly
print(name)

希望这有效:)

可以用类型注解,q可以用猫注解成一个队列:

q = Queue()  # type: Queue[Cat]

q: Queue[Cat] = Queue()