{"id":534,"date":"2019-01-11T00:03:26","date_gmt":"2019-01-10T15:03:26","guid":{"rendered":"https:\/\/www.nblog09.com\/w\/?p=534"},"modified":"2021-11-07T10:35:11","modified_gmt":"2021-11-07T01:35:11","slug":"python_tokushu_method","status":"publish","type":"post","link":"https:\/\/www.nblog09.com\/w\/2019\/01\/11\/python_tokushu_method\/","title":{"rendered":"Python\u306e__\u25cb\u25cb__\u306e\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u306e\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u307e\u3068\u3081\u307e\u3057\u305f"},"content":{"rendered":"<h2 class=\"title-border\"><strong>Python\u306e\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\uff08__repr__,__str__,__bytes__,__bool__,__eq__\uff09\u306e\u5229\u7528\u65b9\u6cd5<\/strong><\/h2>\n<p>python\u306b\u306f\u3001_(\u30a2\u30f3\u30c0\u30fc\u30b9\u30b3\u30a2)\u304c2\u3064\u3067\u59cb\u307e\u308a2\u3064\u3067\u7d42\u308f\u308b\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u3068\u547c\u3070\u308c\u308b\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308b\u3002<\/p>\n<p>\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u660e\u793a\u3057\u306a\u304f\u3066\u3082\u7279\u5b9a\u306e\u51e6\u7406\u3092\u5b9f\u884c\u3057\u305f\u5834\u5408\u306b\u5185\u90e8\u3067\u547c\u3073\u51fa\u3055\u308c\u308b\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001<br \/>\n<span class=\"pink_span\">a + b<\/span><br \/>\n\u3068\u3059\u308b\u3068\u304d\u3001\u5185\u90e8\u3067<br \/>\n<span class=\"pink_span\">a.__add__(b)<\/span><br \/>\n\u3068\u3044\u3046\u3088\u3046\u306ba\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e__add__()\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>__init__, __new__, __del__\u306a\u3069\u3082\u4ee3\u8868\u7684\u306a\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u3067\u3042\u308b\u3002<br \/>\n(\u8a73\u7d30\u306f<a href=\"https:\/\/www.nblog09.com\/w\/2019\/01\/06\/python-constractor\/\">\u3053\u3061\u3089<\/a>)<\/p>\n<p>Python\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308b\u3002<\/p>\n<table class=\"cps-table03\">\n<tbody>\n<td>__repr__<\/td>\n<td class=\"rankinginfo\">repr(object)\u306e\u3068\u304d\u3001\u547c\u3073\u51fa\u3055\u308c\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u60c5\u5831\u3092\u8868\u3059\u6587\u5b57\u5217\u3092\u8fd4\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>__str__<\/td>\n<td class=\"rankinginfo\">str(object),print(object)\u306a\u3069\u547c\u3073\u51fa\u3055\u308c\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6587\u5b57\u5217\u306b\u5909\u63db\u3057\u3066\u8fd4\u3059\u3002<span class=\"pink_span\">__str__\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408__repr__\u304c\u547c\u3073\u51fa\u3055\u308c\u308b<\/span><\/td>\n<\/tr>\n<tr>\n<td>__bytes__<\/td>\n<td class=\"rankinginfo\">bytes(object)\u306a\u3069\u3067\u547c\u3073\u51fa\u3055\u308c\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092bytes\u578b\u3067\u8fd4\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>__hash__<\/td>\n<td class=\"rankinginfo\">\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3067\u8fd4\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>__bool__<\/td>\n<td class=\"rankinginfo\">obj1 or obj2 \u306e\u3088\u3046\u306a\u30d6\u30fc\u30eb\u6f14\u7b97\u3092\u884c\u3046\u3068\u304d\u3001\u307e\u305f\u306fbool(obj)\u306e\u5834\u5408\u306b\u547c\u3073\u51fa\u3055\u308c\u308b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>\u6bd4\u8f03\u6f14\u7b97<\/p>\n<table class=\"cps-table03\">\n<tbody>\n<tr>\n<td>&lt;<\/td>\n<td class=\"rankinginfo\">__lt__(self, object)<\/td>\n<\/tr>\n<tr>\n<td>&lt;=<\/td>\n<td class=\"rankinginfo\">__le__(self, object)<\/td>\n<\/tr>\n<tr>\n<td>==<\/td>\n<td class=\"rankinginfo\">__eq__(self, object)<\/td>\n<\/tr>\n<tr>\n<td>!=<\/td>\n<td class=\"rankinginfo\">__ne__(self, object)<\/td>\n<\/tr>\n<tr>\n<td>&gt;<\/td>\n<td class=\"rankinginfo\">__gt__(self, object)<\/td>\n<\/tr>\n<tr>\n<td>&gt;=<\/td>\n<td>__ge__(self, object)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u306e\u5229\u7528\u4f8b(__repr__)<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nclass Human:<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, name, age):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name = name<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.age = age<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __repr__(self): <strong># repr(Human\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9)\u306e\u969b\u306b\u547c\u3073\u51fa\u3055\u308c\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \"Human\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\"<\/p>\n<p>taro = Human(\"Taro\", 21)<br \/>\njiro = Human(\"Jiro\", 20)<br \/>\ntaro_2 = Human(\"Taro\", 21)<br \/>\nnanashi = Human(\"\", 32)<\/p>\n<p>print(repr(taro)) <strong># Human\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059<\/strong><\/p>\n<\/div>\n<ul>\n<li>\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u306e\u5229\u7528\u4f8b(__str__)<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nclass Human:<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, name, age):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name = name<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.age = age<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __str__(self): <strong> # str(Human\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9)\u306e\u969b\u306b\u547c\u3073\u51fa\u3055\u308c\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \"\u540d\u524d\u306f{}\u3067\u3001\u5e74\u9f62\u306f{}\u3067\u3059\".format(self.name, self.age)<\/p>\n<p>taro = Human(\"Taro\", 21)<br \/>\njiro = Human(\"Jiro\", 20)<br \/>\ntaro_2 = Human(\"Taro\", 21)<br \/>\nnanashi = Human(\"\", 32)<\/p>\n<p>print(str(taro)) <strong># \u540d\u524d\u306fTaro\u3067\u3001\u5e74\u9f62\u306f21\u3067\u3059<\/strong><\/p>\n<\/div>\n<ul>\n<li>\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u306e\u5229\u7528\u4f8b(__bytes__)<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nclass Human:<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, name, age):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name = name<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.age = age<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __bytes__(self):<strong> # bytes(Human\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9)\u306e\u969b\u306b\u547c\u3073\u51fa\u3055\u308c\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \"{}{}\".format(self.name, self.age).encode(\"UTF-8\") <strong># name\u3068age\u3092\u6587\u5b57\u5217\u306b\u3057\u3066\u3001utf-8\u3067encode\u3057\u3066bytes\u914d\u5217\u306b\u3057\u3066\u8fd4\u3059<\/strong><\/p>\n<p>taro = Human(\"Taro\", 21)<br \/>\njiro = Human(\"Jiro\", 20)<br \/>\ntaro_2 = Human(\"Taro\", 21)<br \/>\nnanashi = Human(\"\", 32)<\/p>\n<p>print(bytes(taro)) <strong># b'Taro21'<\/strong><\/p>\n<\/div>\n<ul>\n<li>\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u306e\u5229\u7528\u4f8b(__hash__)<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nclass Human:<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, name, age):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name = name<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.age = age<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __hash__(self): <strong># hash(Human\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9)\u306e\u969b\u306b\u547c\u3073\u51fa\u3055\u308c\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return hash(\"{}{}\".format(self.name, self.age)) <strong># name\u3068age\u3092\u6587\u5b57\u5217\u306b\u3057\u3001hash\u5024\u306b\u3057\u3066\u8fd4\u3059<\/strong><\/p>\n<p>taro = Human(\"Taro\", 21)<br \/>\njiro = Human(\"Jiro\", 20)<br \/>\ntaro_2 = Human(\"Taro\", 21)<br \/>\nnanashi = Human(\"\", 32)<\/p>\n<p>print(hash(taro)) <strong># -3906044921558198740(\u30cf\u30c3\u30b7\u30e5\u5024)<\/strong><\/p>\n<\/div>\n<ul>\n<li>\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u306e\u5229\u7528\u4f8b(__bool__)<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nclass Human:<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, name, age):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name = name<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.age = age<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __bool__(self): <strong># \u540d\u524d\u304c\u3042\u308b\u3068\u304d\u306fTrue\u3001\u540d\u524d\u304c\u306a\u3044\u3068\u304d\u306fFalse\u3092\u8fd4\u3059<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self.name != \"\":<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return True<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return False<\/p>\n<p>taro = Human(\"Taro\", 21)<br \/>\njiro = Human(\"Jiro\", 20)<br \/>\ntaro_2 = Human(\"Taro\", 21)<br \/>\nnanashi = Human(\"\", 32)<\/p>\n<p>if (taro): <strong># AAAAAAAAAA<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;print(\"A\" * 10)<\/p>\n<p>if (nanashi): <strong># \u4f55\u3082\u8868\u793a\u3055\u308c\u306a\u3044<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;print(\"B\" * 10)<\/p>\n<\/div>\n<ul>\n<li>\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u306e\u5229\u7528\u4f8b(__eq__)<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nclass Human:<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, name, age):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name = name<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.age = age<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __eq__(self, other): <strong># object1 == object2\u306e\u6642\u547c\u3073\u51fa\u3055\u308c\u308b\u3002name\u3068age\u304c\u7b49\u3057\u3044\u3068\u304d\u306bTrue\u3092\u8fd4\u3059<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self.name == other.name and self.age == other.age:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return True<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return False<\/p>\n<p>taro = Human(\"Taro\", 21)<br \/>\njiro = Human(\"Jiro\", 20)<br \/>\ntaro_2 = Human(\"Taro\", 21)<br \/>\nnanashi = Human(\"\", 32)<\/p>\n<p>print(taro == jiro) <strong># False<\/strong><br \/>\nprint(taro == taro_2)<strong> # True<\/strong><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Python\u306e\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\uff08__repr__,__str__,__bytes__,__bool__,__eq__\uff09\u306e\u5229\u7528\u65b9\u6cd5 python\u306b\u306f\u3001_(\u30a2\u30f3\u30c0\u30fc\u30b9\u30b3\u30a2)\u304c2\u3064\u3067\u59cb\u307e\u308a2\u3064\u3067\u7d42\u308f\u308b\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u3068\u547c\u3070\u308c\u308b\u30e1\u30bd\u30c3\u30c9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":369,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[9,219],"tags":[14,116],"class_list":{"0":"post-534","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python","8":"category-it-programming","9":"tag-python","10":"tag-116","11":"entry"},"uagb_featured_image_src":{"full":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/krasnal-962528__480.jpg",724,480,false],"thumbnail":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/krasnal-962528__480-150x150.jpg",150,150,true],"medium":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/krasnal-962528__480-300x199.jpg",300,199,true],"medium_large":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/krasnal-962528__480.jpg",724,480,false],"large":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/krasnal-962528__480.jpg",724,480,false],"1536x1536":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/krasnal-962528__480.jpg",724,480,false],"2048x2048":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/krasnal-962528__480.jpg",724,480,false],"home-middle":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/krasnal-962528__480.jpg",528,350,false],"home-top":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/krasnal-962528__480.jpg",634,420,false],"sidebar-thumbnail":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/krasnal-962528__480.jpg",100,66,false]},"uagb_author_info":{"display_name":"9nmatsumoto","author_link":"https:\/\/www.nblog09.com\/w\/author\/9nmatsumoto\/"},"uagb_comment_info":3,"uagb_excerpt":"Python\u306e\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\uff08__repr__,__str__,__bytes__,__bool__,__eq_&hellip;","_links":{"self":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/comments?post=534"}],"version-history":[{"count":16,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/534\/revisions"}],"predecessor-version":[{"id":3554,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/534\/revisions\/3554"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media\/369"}],"wp:attachment":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media?parent=534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/categories?post=534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/tags?post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}