{"id":1432,"date":"2018-01-08T20:49:45","date_gmt":"2018-01-08T11:49:45","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=1432"},"modified":"2018-01-15T23:28:14","modified_gmt":"2018-01-15T14:28:14","slug":"setattr%e5%b1%9e%e6%80%a7%e3%81%ae%e8%bf%bd%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/1432","title":{"rendered":"setattr \u5c5e\u6027\u306e\u8ffd\u52a0"},"content":{"rendered":"<p>Python\u306f\u300c.\u300d\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5c5e\u6027\u306b\u76f4\u63a5\u8ffd\u52a0\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u7d44\u307f\u8fbc\u307f\u95a2\u6570\u306esetattr\u3001getattr\u3001delattr\u3092\u4f7f\u7528\u3059\u308b\u3068\u660e\u793a\u7684\u306b\u5c5e\u6027\u306e\u64cd\u4f5c\u3092\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>setattr<\/h2>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5c5e\u6027\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u7b2c1\u5f15\u6570\u306b\u5bfe\u8c61\u3068\u306a\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3001\u7b2c2\u5f15\u6570\u306b\u5c5e\u6027\u540d\u3001\u7b2c3\u5f15\u6570\u306b\u5c5e\u6027\u5024\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass User():\r\n    pass\r\n\r\nobj = User()\r\nsetattr(obj, &quot;name&quot;, &quot;Kuro&quot;)\r\n\r\nprint(obj.name)\r\n\r\n<\/pre>\n<p>.\u3067\u8ffd\u52a0\u3057\u3066\u3082\u826f\u3044\u306e\u3067\u3059\u304c\u3001setattr\u306fdict\u578b\u306e\u30eb\u30fc\u30d7\u3068\u76f8\u6027\u304c\u826f\u3044\u3067\u3059\u3002\u3088\u304f\u898b\u304b\u3051\u308b\u30c6\u30af\u30cb\u30c3\u30af\u3068\u3057\u3066\u3001\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u751f\u6210\u6642\u306b\u5c5e\u6027\u304c\u591a\u304f\u306a\u308b\u5834\u5408\u3001\u5f15\u6570\u3092dict\u306b\u3057\u3066\u5c5e\u6027\u3092\u8ffd\u52a0\u3059\u308b\u3001\u3068\u3044\u3046\u65b9\u6cd5\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass User():\r\n    def __init__(self, attrs):\r\n        for k, v in attrs.items():\r\n            setattr(self, k, v)\r\n\r\nobj = User({&quot;name&quot;: &quot;Kuro&quot;, &quot;age&quot;: 30, &quot;memo&quot;: &quot;very cool!&quot;})\r\nprint(obj.name)\r\n# Kuro\r\nprint(obj.age)\r\n# 30\r\nprint(obj.memo)\r\n# very cool!\r\n\r\n<\/pre>\n<p>\u5c5e\u6027\u304c\u591a\u5c90\u306b\u308f\u305f\u308b\u5834\u5408\u3001\u5f15\u6570\u3067\u6307\u5b9a\u3059\u308b\u3068\u81a8\u5927\u306b\u306a\u308a\u307e\u3059\u304c\u3001\u3053\u306e\u65b9\u6cd5\u306a\u3089\u3070\u53ef\u5909\u3067\u69d8\u3005\u306a\u5c5e\u6027\u3092\u4ed8\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>\u88dc\u8db3 getattr\u3068delattr<\/h2>\n<p>\u88dc\u8db3\u3068\u3057\u3066\u3001setattr\u306e\u5144\u5f1f\u3001getattr\u3068delattr\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<h3>getattr \u5c5e\u6027\u306e\u53d6\u5f97<\/h3>\n<p>\u5f15\u6570\u3067\u6307\u5b9a\u3057\u305f\u5c5e\u6027\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002\u7b2c1\u5f15\u6570\u3067\u5bfe\u8c61\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u3001\u7b2c2\u5f15\u6570\u3067\u5c5e\u6027\u540d\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u7b2c3\u5f15\u6570\u3067\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u8a2d\u5b9a\u3067\u304d\u308b\u306e\u304c\u30e1\u30ea\u30c3\u30c8\u3067\u3059\u3002<\/p>\n<p>\u5f53\u8a72\u306e\u5c5e\u6027\u304c\u306a\u3044\u5834\u5408\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u304c\u8fd4\u3055\u308c\u308b\u305f\u3081\u3001\u5c5e\u6027\u5b58\u5728\u691c\u67fb\u3092\u4e8b\u524d\u306b\u3059\u308b\u5fc5\u8981\u304c\u306a\u304f\u306a\u308b\u308f\u3051\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001getattr(obj, 'name')\u306f obj.name\u3068\u7b49\u4fa1\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001obj\u306bname\u3068\u3044\u3046\u5c5e\u6027\u304c\u306a\u3044\u3053\u3068\u304c\u4e88\u60f3\u3055\u308c\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u8a2d\u5b9a\u3057\u305f\u3044\u5834\u5408\u306fgetattr(obj, 'name', 'Nameless')\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u304c\u306a\u304f\u3001\u6307\u5b9a\u3057\u305f\u5c5e\u6027\u304c\u306a\u3044\u5834\u5408hAttributeError\u304c\u9001\u51fa\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>delattr \u5c5e\u6027\u306e\u524a\u9664<\/h3>\n<p>\u5c5e\u6027\u3092\u524a\u9664\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001obj\u3068\u3044\u3046\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304b\u3089name\u3068\u3044\u3046\u5c5e\u6027\u3092\u524a\u9664\u3059\u308b\u5834\u5408\u3001delattr(obj, \"name\")\u3068\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u306f\u300c.\u300d\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5c5e\u6027\u306b\u76f4\u63a5\u8ffd\u52a0\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u7d44\u307f\u8fbc\u307f\u95a2\u6570\u306esetattr\u3001getattr\u3001delattr\u3092\u4f7f\u7528\u3059\u308b\u3068\u660e\u793a\u7684\u306b\u5c5e\u6027\u306e\u64cd\u4f5c\u3092\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 setattr \u30aa\u30d6\u30b8\u30a7\u30af...<\/p>\n","protected":false},"author":1,"featured_media":3626,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[136],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1432"}],"collection":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/comments?post=1432"}],"version-history":[{"count":7,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1432\/revisions"}],"predecessor-version":[{"id":3625,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1432\/revisions\/3625"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/3626"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=1432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=1432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=1432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}