如何访问对应于该属性的名称对象属性指定字符串 [英] How to access object attribute given string corresponding to name of that attribute

查看:91
本文介绍了如何访问对应于该属性的名称对象属性指定字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么设置/由 X 获得 T 赋予属性的值。

 类测试():
   ATTR1 = INT
   attR2位= INTT =测试()
X =ATTR1


解决方案

有内置​​的函数调用 GETATTR SETATTR

  GETATTR(对象,attrname)
SETATTR(对象,attrname,价值)

在这种情况下

  X = GETATTR(T,ATTR1​​)
SETATTR(T,'ATTR1,21)

How do you set/get the values of attributes of t given by x.

class test():
   attr1 = int
   attr2 = int

t = test()
x = "attr1"

解决方案

There is built-in functions called getattr and setattr

getattr(object, attrname)
setattr(object, attrname, value)

In this case

x = getattr(t, "attr1")
setattr(t, 'attr1', 21)

这篇关于如何访问对应于该属性的名称对象属性指定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆