{"id":4806,"date":"2019-06-03T17:19:19","date_gmt":"2019-06-03T08:19:19","guid":{"rendered":"https:\/\/blog.bitmeister.jp\/?p=4806"},"modified":"2022-08-09T17:41:57","modified_gmt":"2022-08-09T08:41:57","slug":"python-%e3%81%a7%e3%82%b7%e3%83%b3%e3%82%b0%e3%83%ab%e3%83%88%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3%e3%82%92%e5%ae%9f%e8%a3%85%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/blog.bitmeister.jp\/?p=4806","title":{"rendered":"Python \u3067\u30b7\u30f3\u30b0\u30eb\u30c8\u30f3\u30d1\u30bf\u30fc\u30f3\u3092\u5b9f\u88c5\u3059\u308b"},"content":{"rendered":"<p>\u751f\u6210\u3055\u308c\u308b\u30af\u30e9\u30b9\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u5e38\u306b\u4e00\u3064\u306b\u3059\u308b\u30b7\u30f3\u30b0\u30eb\u30c8\u30f3\u30d1\u30bf\u30fc\u30f3\u3092 Python \u3067\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n<p>Python \u306f Java \u307f\u305f\u3044\u306b &#8220;private static&#8221; \u307f\u305f\u3044\u306a\u4fee\u98fe\u304c\u3067\u304d\u306a\u3044\u3057\u3001\u3069\u3053\u3067\u521d\u671f\u5316\u3059\u308c\u3070\u3044\u3044\u306e\u304b\u3082\u3061\u3087\u3063\u3068\u8003\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p><!--more--><br \/>\n\u307e\u305a\u30c0\u30e1\u306a\u30d1\u30bf\u30fc\u30f3\u304b\u3089\u3002<\/p>\n<pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\r\nclass Sandwich:\r\n    singleton = Sandwich()\r\n\r\n# \u3053\u308c\u306f NameError: name 'Sandwich' is not defined \u3067\u5931\u6557\u3059\u308b\u3002\r\n<\/pre>\n<p>Python \u3067\u306f\u3053\u306e\u30b9\u30b3\u30fc\u30d7\u3067\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u300c\u30af\u30e9\u30b9\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u306a\u3044\u300d\u3068\u3057\u3066\u5931\u6557\u3057\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f __init__() \u30e1\u30bd\u30c3\u30c9\u306f\u3067\u3057\u3087\u3046\u304b\u3002\u3053\u306e\u30a2\u30a4\u30c7\u30a2\u3082\u30c0\u30e1\u3067\u3059\u3002\u306a\u305c\u306a\u3089 __init__() \u30e1\u30bd\u30c3\u30c9\u306e\u5f15\u6570\u306b\u6e21\u3055\u308c\u308b self \u306f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u3059\u3088\u306d\u3002 __init__() \u30e1\u30bd\u30c3\u30c9\u304c\u30b3\u30fc\u30eb\u3055\u308c\u305f\u6642\u70b9\u3067\u3001\u65e2\u306b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u4f5c\u3089\u308c\u3066\u3057\u307e\u3063\u3066\u3044\u307e\u3059\u3002\u8907\u6570\u56de\u547c\u3070\u308c\u305f\u3089\u8907\u6570\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u751f\u6210\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u3063\u3066\u3001\u30b7\u30f3\u30b0\u30eb\u30c8\u30f3\u306b\u306a\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u305d\u3053\u3067 __new__() \u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002 __init__() \u30e1\u30bd\u30c3\u30c9\u3088\u308a\u524d\u306b\u547c\u3070\u308c\u3001\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3057\u3066\u8fd4\u3059\u306e\u304c __new__() \u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002\u5358\u306b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3057\u3066\u8fd4\u3059\u3060\u3051\u306e\u5834\u5408\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n<pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\r\nclass Sandwich:\r\n    def __new__(cls, *args, **kwargs):\r\n        # \u89aa\u30af\u30e9\u30b9\u306e __new__() \u306b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u3063\u3066\u3082\u3089\u3044\u3001\u305d\u306e\u307e\u307e\u8fd4\u3059\r\n        return super(Sandwich, cls).__new__(cls)\r\n<\/pre>\n<p>\u3064\u307e\u308a __new__() \u30e1\u30bd\u30c3\u30c9\u304c\u8fd4\u3059\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4e00\u3064\u3060\u3051\u306b\u3059\u308c\u3070\u3001\u30b7\u30f3\u30b0\u30eb\u30c8\u30f3\u30d1\u30bf\u30fc\u30f3\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\u305d\u308c\u304c\u3053\u3061\u3089\u3002<\/p>\n<pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\r\n# Singleton\r\n\r\nimport threading\r\n\r\nclass Sandwich:\r\n    __singleton = None\r\n    __new_lock = threading.Lock()\r\n    # \u5909\u6570\u540d\u306b &quot;__&quot; (\u30a2\u30f3\u30c0\u30fc\u30b9\u30b3\u30a2\u3092\uff12\u3064) \u3092\u3064\u3051\u308b\u3068\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u5909\u6570\u306b\u306a\u308b\r\n\r\n    def __new__(cls, *args, **kwargs):\r\n        cls.__new_lock.acquire() # \u30ed\u30c3\u30af\u53d6\u5f97\r\n        if cls.__singleton == None:\r\n            cls.__singleton = super(Sandwich, cls).__new__(cls)\r\n        cls.__new_lock.release() # \u30ed\u30c3\u30af\u958b\u653e\r\n        return cls.__singleton\r\n<\/pre>\n<p>\u30af\u30e9\u30b9\u5909\u6570\u306b\u306f\u3058\u3081\u3066\u751f\u6210\u3057\u305f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4ee3\u5165\u3057\u3066\u304a\u3044\u3066\u3001\u305d\u308c\u3092\u4f7f\u3044\u307e\u308f\u3057\u307e\u3059\u3002\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u3067\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u8907\u6570\u4f5c\u3089\u308c\u306a\u3044\u3088\u3046\u306b\u300c\u65e2\u5b58\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u30c1\u30a7\u30c3\u30af\u300d\u3068\u300c\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u751f\u6210\u300d\u306b\u518d\u5165\u3055\u308c\u306a\u3044\u3088\u3046\u306b\u6392\u4ed6\u5236\u5fa1\u3057\u3066\u3044\u307e\u3059\u3002\u305f\u3060\u3001\uff12\u56de\u76ee\u4ee5\u964d\u306f\u518d\u5165\u3055\u308c\u3066\u3066\u3082\u554f\u984c\u306a\u3044\u306e\u3067\u3001\u7121\u99c4\u306a\u6392\u4ed6\u5236\u5fa1\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3001\u4f8b\u3048\u3070\u30a4\u30f3\u30dd\u30fc\u30c8\u6642\u306b\u5fc5\u305a1\u56de\u3060\u3051\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u3063\u3066\u304a\u3044\u3066\u3001\u3042\u3068\u306f\u30ed\u30c3\u30af\u30d5\u30ea\u30fc\u3067\u3082\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\r\n# Singleton'\r\n\r\nclass Sandwich:\r\n    __singleton = None\r\n\r\n    def __new__(cls, *args, **kwargs):\r\n        if cls.__singleton == None:\r\n            cls.__singleton = super(Sandwich, cls).__new__(cls)\r\n        return cls.__singleton\r\n\r\nSandwich() # \u30af\u30e9\u30b9\u5b9a\u7fa9\u306e\u76f4\u4e0b\u306b\u66f8\u3044\u3066\u3001\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u308b\u305f\u3081\u306b\u5fc5\u305a\u547c\u3076\r\n<\/pre>\n<p>\u6700\u5f8c\u306b\u3001\u672c\u5f53\u306b\u30b7\u30f3\u30b0\u30eb\u30c8\u30f3\u306b\u306a\u3063\u3066\u3044\u308b\u304b\u78ba\u8a8d\u3059\u308b\u30b3\u30fc\u30c9\u3092\u8cbc\u3063\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; class Sandwich:\r\n...     __singleton = None\r\n...     __spam = None\r\n...     def __new__(cls, *args, **kwargs):\r\n...         if cls.__singleton == None:\r\n...             cls.__singleton = super(Sandwich, cls).__new__(cls)\r\n...         return cls.__singleton\r\n...     def set_spam(self, spam=None):\r\n...         self.__spam = spam\r\n...     def get_spam(self):\r\n...         return self.__spam\r\n... \r\n&gt;&gt;&gt; Sandwich()\r\n&lt;__main__.Sandwich object at 0x76ad5630&gt;\r\n&gt;&gt;&gt; sw1 = Sandwich()\r\n&gt;&gt;&gt; sw2 = Sandwich()\r\n&gt;&gt;&gt; sw1\r\n&lt;__main__.Sandwich object at 0x76ad5630&gt;\r\n&gt;&gt;&gt; sw2\r\n&lt;__main__.Sandwich object at 0x76ad5630&gt;\r\n&gt;&gt;&gt; sw2.set_spam('spam spam spam')\r\n&gt;&gt;&gt; sw1.get_spam()\r\n'spam spam spam'\r\n<\/pre>\n<p>\u3046\u3048\u306e sw1 \u3068 sw2 \u306f\u540c\u3058\u3082\u306e\u3060\u3068\u3044\u3046\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u751f\u6210\u3055\u308c\u308b\u30af\u30e9\u30b9\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u5e38\u306b\u4e00\u3064\u306b\u3059\u308b\u30b7\u30f3\u30b0\u30eb\u30c8\u30f3\u30d1\u30bf\u30fc\u30f3\u3092 Python \u3067\u5b9f\u88c5\u3057\u307e\u3059\u3002 Python \u306f Java \u307f\u305f\u3044\u306b &#8220;private static&#8221; \u307f\u305f\u3044\u306a\u4fee\u98fe\u304c\u3067\u304d\u306a\u3044\u3057\u3001 [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4806","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts\/4806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4806"}],"version-history":[{"count":10,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts\/4806\/revisions"}],"predecessor-version":[{"id":5147,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts\/4806\/revisions\/5147"}],"wp:attachment":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}