首页 > 解决方案 > python变量声明理解困难

问题描述

我是 Python 的新手,并试图解码一行以移植到 Go。在当前的 python 脚本中,我在大多数类下都遇到了这个声明(dataValue)。

class Keys(object):
   AB1 = 0x0FF
   AB2 = 0x0A2
   dataValue = {'0xffff':None}

   @classmethod
   def _init(cls):
     for ke, vl in vars(cls).items():
       
  1. 什么是数据值?0xffff 是键并保持 None 作为值吗?
  2. vars(cls).items() 返回什么?
  3. @classmethod 意味着这个类键只使用一次?(我遇到了很多@propoerty 的定义,@classmthods 这些关键字仍然让我感到困惑)

试图通过实际项目来理解这些概念。提前致谢!

标签: python

解决方案


  1. dataValue是一个字典变量名称,其中包含key:value一对'0xffff':None https://docs.python.org/3/tutorial/datastructures.html
  2. vars(cls).items()返回具有属性https://docs.python.org/3/library/functions.html#vars__dict__的模块、类、实例或任何其他对象的属性。__dict__
  3. 返回另一个函数的函数,通常使用 @wrapper 语法作为函数转换应用。装饰器的常见示例是 classmethod() 和 staticmethod() https://docs.python.org/3/glossary.html#term-decorator https://docs.python.org/3/library/functions.html#classmethod https://docs.python.org/3/library/functions.html#staticmethod

推荐阅读