原文

转载请注明出处:首发于CSDN

访问一个已知属性名的属性很简单,a.name就完事了,然而访问不知名的属性却相对麻烦(大概不会有码农不知道自己的类里都有什么属性吧)。以防万一,写了一点东西提供一个思路。

该方法可以以键值对(字典)形式导出该类下所有属性及对应值,不会导出方法或者别的什么东西,比较方便。

相似地,也可以用一个键值对(需要保存在类对象的一个临时属性中,当然,修改成方法的参数形式难度也不大)对此进行修改。

代码如下:

# Reference 1: http://c.biancheng.net/view/2374.html
class Article():def __init__(self):self.sample1 = ['abc']self.sample2 = 0xFFself.sample3 = '@ClementLevi'self._content = {} # 临时容器,届时不纳入考虑范围内def outputAttrs(self):for attr in self.__dict__: # __dict__属性(一个字典)不会包含父类属性和各种方法,因而十分好用if attr != '_content': # 不导出临时容器,避免递归self._content.update({attr: self.__dict__[attr]}) #此处借鉴参考文献1,实际上并不完全相同;而且后文中修改值的手段也与我此处所写的有很大区别print(self._content)return 0def inputAttrs(self):for attr in self._content.keys(): # dict.keys()会返回一个'dict_keys'类型对象,使用for遍历即可获取各字符串# 此处需要注意,临时容器的名称被写死,不过修改难度不大setattr(self, attr, (self._content)[attr]) # 使用setattr()函数设置属性值return 0# 测试
if __name__ == "__main__":a = Article()print(a.outputAttrs())a._content = {"What":114514}print(a.outputAttrs())a.inputAttrs()print(a.What)

参考了一点点C语言中文网上的东西,不过不多,而且实现手段也不同。参考文献URL见代码首行和文末。

参考文献:
[1]:Python __dict__属性:查看对象内部所有属性名和属性值组成的字典

English Version

Translated by myself from Chinese version. If confused, please refer to the original text.

To acquire attributes of a class object when knowing their names is quite simple – an “a.name” can do. But it is not the case when the names remain unknown (I suppose few can be so careless to forget the names!). In case, here goes a way to solve this problem.

This example shows how key-value pairs about attributes can be digested from a class object without outputting “method” and other things.

Correspondingly, a key-value pair can modify those attributes as well, as long as it is written into a temporary attribute, which can surely be modified into a parameter of this method for other purpose.

The code is as follows:

# Reference 1: http://c.biancheng.net/view/2374.html
class Article():def __init__(self):self.sample1 = ['abc']self.sample2 = 0xFFself.sample3 = '@ClementLevi'self._content = {} # Temporary container which won't outputdef outputAttrs(self):for attr in self.__dict__: # __dict__ won't contain father_class attributes or methods, making it quite convenientif attr != '_content': # Do not output the temp container in case of recursionself._content.update({attr: self.__dict__[attr]}) # Referred to reference_1 but not actually the same. The modification later on is not a same way as wellprint(self._content)return 0def inputAttrs(self):for attr in self._content.keys(): # dict.keys() returns a 'dict_keys' object,which can use 'for' upon for each string# Attention: the container's name is written, but can certainly be changed when you use this codesetattr(self, attr, (self._content)[attr]) # use "setattr()" to set valuesreturn 0# testing
if __name__ == "__main__":a = Article()print(a.outputAttrs())a._content = {"What":114514}print(a.outputAttrs())a.inputAttrs()print(a.What)

Not too much reference, and without the same technique to make this.
Reference:
[1]:Python __dict__属性:查看对象内部所有属性名和属性值组成的字典

Python - 一种一次性导出或修改所有类对象属性与值的方法相关推荐

  1. python添加、修改、删除、访问类对象属性的2种方法

    1.直接添加.修改.删除.访问类对象属性 class Employee (object):empCount = 0 def __init__(self, name, salary) :self.nam ...

  2. Oracle入门(十二G1)修改序列(Sequence)起始值的方法

    转载自 Oracle修改序列(Sequence)起始值的方法 Oracle 序列(Sequence)主要用于生成流水号,在应用中经常会用到,特别是作为ID值,拿来做表主键使用较多. 但是,有时需要修改 ...

  3. JQ无法修改input的type属性的替代解决方法

    JQ无法修改input的type属性的替代解决方法 参考文章: (1)JQ无法修改input的type属性的替代解决方法 (2)https://www.cnblogs.com/wsun/p/56300 ...

  4. python类定义中、对象字符串的特殊方法是_python中自定义类对象json字符串化的方法_python json转字符串、...

    python中自定义类对象json字符串化的方法 1. 用 json 或者simplejson 就可以 2.定义转换函数: def convert_to_builtin_type(obj): prin ...

  5. win8系统修改默认html,win8系统如何修改ie浏览器的主页默认值? 修改ie浏览器的主页默认值的方法介绍...

    win8系统设置ie浏览器的主页方法: 1.IE默认连接首页被修改,受到更改的注册表项目为: HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explor ...

  6. python怎样遍历列表中数字_Python 遍历列表里面序号和值的方法(三种)

    三种遍历列表里面序号和值的方法: 最近学习python这门语言,感觉到其对自己的工作效率有很大的提升,特在情人节这一天写下了这篇博客,下面废话不多说,直接贴代码 #!/usr/bin/env pyth ...

  7. python识别人脸多种属性_9种人脸情绪识别、22类人脸属性、 15类皮肤质量识别,竹间智能要让情感机器人更理解人...

    撰文 | 邱陆陆 编辑 | 藤子 「我们希望做情感机器人,希望透过机器更智能地理解一个人.」随着计算能力的提升.神经网络的步步深入,配以图像识别.语音识别的人工智能已经逐渐变得能够「听见」.「看见」. ...

  8. python文件命名 数字_python 批量修改数字类的文件名

    今天碰到一个小问题,下载音频的时候,文件名的名字变成了数字,排序呢,是按照数字的大小往下排的. 想自己给它们重新起名字,但是又不打乱音频的顺序.好吧,那就自己写写代码吧. 思路就是遍历音频文件的数字文 ...

  9. 【Javascript】通过实例对象修改原型对象属性的值

    结论: 当原型对象的属性值为基本数据类型时,无法通过实例对象直接修改原型对象的属性值.当原型对象的属性值为引用数据类型时,可以直接通过实例对象修改原型对象的属性值. 基本类型的数据值有7种:字符串(S ...

最新文章

  1. Linux下Mysql数据库的基础操作
  2. T-SQL RAND()
  3. mysql 行列转换 动态_mysql 行列动态转换的实现(列联表,交叉表)
  4. 【CodeForces - 987C 】Three displays (dp,最长上升子序列类问题,三元组问题)
  5. echarts数据怎么赋值 vue_vue中 怎么把echarts中data的数据循环
  6. wms仓储系统培训_WMS系统在仓储管理中产生的盈利
  7. tcp 四次挥手_TCP三次握手和四次挥手通俗理解
  8. c#复制包含子目录文件夹代码
  9. 招聘:以考试为主,面试为辅
  10. 目标追踪论文之狼吞虎咽(4):MCCT算法
  11. 六、小程序|App抓包-移动端抓包app-抓包
  12. Codejock Toolkit工具包专业版
  13. Layui-select 下拉框实现拼音全拼匹配/首字母模糊搜索
  14. app打开QQ与陌生人聊天
  15. 2019上半年软件设计师考试体会
  16. 计算机和人脑在线阅读,人脑与电脑
  17. 华为浏览器html页样式没了,返回键没有了怎么办 华为手机没有返回键了怎么处理...
  18. android RatingBar基本使用介绍
  19. MTK平台 获取本机的SIM卡中IMSI号
  20. 2018春招便利蜂--你的准备永远不能覆盖面试官的提问

热门文章

  1. 爱奇艺2018.9.28笔试 清雨的自助餐
  2. Android 仿微信朋友圈拍小视频上传到服务器
  3. 华为服务器cpu是否支持虚拟化,处理器配置 - 华为服务器 Brickland平台 BIOS 参数参考 30 - 华为...
  4. 【数据结构之并查集】并查集详解(零基础入门,超级有趣的!!!)
  5. 苹果天气不显示_苹果收购天气应用Dark Sky,安卓用户不能再使用
  6. iOS进阶4 -- 动画总结
  7. 华为p9 android版本,华为P9有几个版本 华为P9各版本区别对比 (全文)
  8. 如何通过微调整功能,解决打印位置上下偏移的问题?(转)
  9. 帝国导航栏如何实现高亮显示
  10. 路径规划 | 图搜索算法:JPS