【问题标题】:Pycharm lose object indicator after put and get from queuePycharm在放入并从队列中获取后丢失对象指示符
【发布时间】:2021-03-02 21:29:23
【问题描述】:

我知道这不是一个大问题。但是当我的对象有很多变量并且每个变量都很长时,它变得很烦人。有什么方法可以让 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 python-3.x pycharm queue


    【解决方案1】:

    可以使用类型注解,q 可以注解为带有猫的队列:

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

    q: Queue[Cat] = Queue()
    

    【讨论】:

      【解决方案2】:

      至少在 python 中。除非您使用 global 在您的情况下可以使用类似的东西,否则您无法从类内部访问变量。

      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)
      

      希望这可行:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-10
        • 2021-12-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多