{"id":338,"date":"2017-02-21T21:56:41","date_gmt":"2017-02-21T12:56:41","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=338"},"modified":"2021-11-06T21:07:16","modified_gmt":"2021-11-06T12:07:16","slug":"%e5%9f%ba%e6%9c%ac%e7%9a%84%e3%81%aa%e7%89%b9%e6%ae%8a%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/338","title":{"rendered":"\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9"},"content":{"rendered":"<h2>\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9<\/h2>\n<p>Python\u306e\u30af\u30e9\u30b9\u3067\u306f\u3001\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u3068\u547c\u3070\u308c\u308b\u30a2\u30f3\u30c0\u30fc\u30d0\u30fc2\u3064\u3067\u56f2\u307e\u308c\u305f\u7279\u5b9a\u306e\u540d\u524d\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3059\u308b\u3068\u3001\u3042\u308b\u51e6\u7406\u3092\u5b9f\u884c\u3057\u305f\u969b\u306b\u305d\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u81ea\u52d5\u7684\u306b\u547c\u3073\u51fa\u3055\u308c\u307e\u3059\u3002\u305f\u3068\u3048\u3070__init__\u304c\u305d\u308c\u306b\u8a72\u5f53\u3057\u307e\u3059\u306d\u3002\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u751f\u6210\u6642\u306b\u81ea\u52d5\u7684\u306b\u547c\u3073\u3060\u3055\u308c\u307e\u3059\u3002<\/p>\n<h2>\u57fa\u672c\u7684\u306a\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u306e\u7d39\u4ecb<\/h2>\n<p>\u304b\u306a\u308a\u306e\u7a2e\u985e\u304c\u3042\u308b\u305f\u3081\u3053\u3053\u3067\u306f\u7c21\u5358\u306b\u7d39\u4ecb\u3059\u308b\u306b\u3068\u3069\u3081\u307e\u3059\u3002\u307e\u305f\u3001\u6b21\u30da\u30fc\u30b8\u4ee5\u964d\u3067\u7d39\u4ecb\u3059\u308b\u30c7\u30a3\u30b9\u30af\u30ea\u30d7\u30bf\u3084\u6f14\u7b97\u306e\u5b9a\u7fa9\u3067\u4f7f\u7528\u3059\u308b\u3082\u306e\u3082\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u306e\u4e00\u7a2e\u3067\u3059\u3002<\/p>\n<h3>__repr__<\/h3>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u60c5\u5831\u3092\u8868\u3059\u6587\u5b57\u5217\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>__str__<\/h3>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6587\u5b57\u5217\u306b\u5909\u63db\u3057\u3066\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>__bool__(self)<\/h3>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u771f\u7406\u5024\u306e\u8a55\u4fa1\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h2>__str__\u3068__repr__<\/h2>\n<p>\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u306e__str__\u3068__repr__\u3092\u5b9f\u88c5\u3057\u305f\u30af\u30e9\u30b9\u306e\u30b5\u30f3\u30d7\u30eb\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\nclass User():\r\n    &quot;&quot;&quot;\r\n    \u30e6\u30fc\u30b6\u30fc\u30af\u30e9\u30b9\r\n    &quot;&quot;&quot;\r\n\r\n    def __init__(self, name=&quot;&quot;, age=0):\r\n        self.name = name\r\n        self.age = age\r\n\r\n    def say_hello(self):\r\n        print(&quot;Hello, my name is &quot; + self.name)\r\n\r\n    def __str__(self):\r\n        return &quot;User:&quot; + self.name\r\n\r\n    def __repr__(self):\r\n        return str({&quot;name&quot;: self.name, &quot;age&quot;: str(self.age)})\r\n\r\n\r\nuser = User(&quot;Kuro&quot;, 30)\r\n\r\nprint(user)  # print(str(user))\u3068\u540c\u3058\r\n# User:Kuro\r\n\r\nprint(repr(user))\r\n# {'name': 'Kuro', 'age': '30'}\r\n\r\n\r\n<\/pre>\n<p>str\u306e\u5f15\u6570\u306bUser\u578b\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u5b9a\u3059\u308b\u3068\u3001__str__\u3067\u5b9f\u88c5\u3057\u305f\u5185\u5bb9\u306e\u6587\u5b57\u5217\u8868\u73fe\u304c\u53d6\u5f97\u3067\u304d\u307e\u3057\u305f\u3002\u305d\u3057\u3066\u3001\u524d\u8ff0\u306e\u901a\u308aprint\u95a2\u6570\u306f\u5185\u90e8\u7684\u306bstr\u3092\u547c\u3073\u51fa\u3059\u305f\u3081\u3001\u30b3\u30fc\u30c9\u4e2d\u306e\u30b3\u30e1\u30f3\u30c8\u306e\u901a\u308aprint(str(user))\u3068\u3057\u3066\u3082\u540c\u69d8\u306e\u7d50\u679c\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001repr\u95a2\u6570\u3092\u4f7f\u7528\u3059\u308b\u3068__repr__\u3067\u8fd4\u3055\u308c\u308b\u5024\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u306a\u304a\u3001__str__\u3092\u5b9f\u88c5\u305b\u305a\u306b__repr__\u306e\u307f\u5b9f\u88c5\u3059\u308b\u3068\u3001str\u95a2\u6570\u5b9f\u884c\u6642\u306b__repr__\u306e\u5024\u304c\u53d6\u5f97\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u305f\u3081\u3001__str__\u306f__repr__\u306e\u7c21\u6613\u7248\u3068\u6349\u3048\u3066\u3082\u826f\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n<p>\u72ec\u81ea\u306b\u30af\u30e9\u30b9\u5b9a\u7fa9\u3092\u3059\u308b\u5834\u5408\u3001\u30c7\u30d0\u30c3\u30b0\u3084\u30ed\u30ae\u30f3\u30b0\u306e\u52b9\u7387\u304c\u4e0a\u304c\u308b\u305f\u3081__repr__\u82e5\u3057\u304f\u306f__str__\u3092\u5b9f\u88c5\u3059\u308b\u3088\u3046\u306b\u3057\u305f\u307b\u3046\u304c\u3088\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>__bool__<\/h2>\n<p>__bool__\u3092\u5b9f\u88c5\u3059\u308b\u3068\u3001bool\u95a2\u6570\u3092\u5b9f\u884c\u3057\u305f\u969b\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u771f\u7406\u5024\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30b5\u30f3\u30d7\u30eb\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass Coordinate:\r\n    def __init__(self, x, y):\r\n        self.x = x\r\n        self.y = y\r\n\r\n    def __bool__(self):\r\n        if self.x or self.y:\r\n            return True\r\n        else:\r\n            return False\r\n\r\n\r\np0 = Coordinate(0, 0)\r\np1 = Coordinate(100, 200)\r\np2 = Coordinate(0, 200)\r\n\r\nif p0:\r\n    print(&quot;\u70b9p0\u306f\u771f\u3068\u8a55\u4fa1\u3055\u308c\u307e\u3057\u305f&quot;)\r\n\r\nif p1:\r\n    print(&quot;\u70b9p1\u306f\u771f\u3068\u8a55\u4fa1\u3055\u308c\u307e\u3057\u305f&quot;)\r\n\r\nif p2:\r\n    print(&quot;\u70b9p2\u306f\u771f\u3068\u8a55\u4fa1\u3055\u308c\u307e\u3057\u305f&quot;)\r\n\r\n# \u5b9f\u884c\u7d50\u679c\r\n# \u70b9p1\u306f\u771f\u3068\u8a55\u4fa1\u3055\u308c\u307e\u3057\u305f\r\n# \u70b9p2\u306f\u771f\u3068\u8a55\u4fa1\u3055\u308c\u307e\u3057\u305f\r\n\r\n<\/pre>\n<p>\u4e0a\u306e\u30b3\u30fc\u30c9\u3067\u306f2\u6b21\u5143\u5e73\u9762\u4e0a\u306e\u5ea7\u6a19\u3092\u8868\u3059Coordinate\u30af\u30e9\u30b9\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002\u70b9\u306e\u5ea7\u6a19\u304c\u3068\u3082\u306b0\u3001\u3064\u307e\u308a\u539f\u70b9\u4ee5\u5916\u306f\u771f\u3068\u8a55\u4fa1\u3055\u308c\u307e\u3059\u3002\u5b9f\u969b\u3001\u30b3\u30fc\u30c9\u5f8c\u65b9\u306b\u3066\u539f\u70b9p0\u3001\u305d\u308c\u4ee5\u5916\u306ep1\u3001p2\u306e\u771f\u7406\u5024\u3092\u8a55\u4fa1\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u539f\u70b9\u306e\u5834\u5408\u306f\u507d\u3068\u8a55\u4fa1\u3055\u308c\u308b\u3053\u3068\u304c\u78ba\u8a8d\u3066\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9 Python\u306e\u30af\u30e9\u30b9\u3067\u306f\u3001\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u3068\u547c\u3070\u308c\u308b\u30a2\u30f3\u30c0\u30fc\u30d0\u30fc2\u3064\u3067\u56f2\u307e\u308c\u305f\u7279\u5b9a\u306e\u540d\u524d\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3059\u308b\u3068\u3001\u3042\u308b\u51e6\u7406\u3092\u5b9f\u884c\u3057\u305f\u969b\u306b\u305d\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u81ea\u52d5\u7684\u306b\u547c\u3073\u51fa\u3055\u308c\u307e\u3059\u3002\u305f\u3068\u3048\u3070__init__\u304c\u305d\u308c\u306b\u8a72\u5f53\u3057...<\/p>\n","protected":false},"author":1,"featured_media":3674,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[103,112],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/338"}],"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=338"}],"version-history":[{"count":7,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/338\/revisions"}],"predecessor-version":[{"id":3713,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/338\/revisions\/3713"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/3674"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}