{"id":295534,"date":"2019-12-17T16:18:46","date_gmt":"2019-12-17T16:18:46","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=295534"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=295534","title":{"rendered":"\u0418\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 Python"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/480022\/\">\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441 <b>Foo<\/b>\u00a0:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class Foo(object):     def __init__(self, x, y=0):         self.x = x         self.y = y<\/code><\/pre>\n<p>  <\/p>\n<p>\u0427\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0435 \u0435\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442?<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">f = Foo(1, y=2)<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 <b>Foo<\/b>? \u0411\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u043d\u043e\u0432\u0438\u0447\u043a\u043e\u0432, \u0434\u0430 \u0438, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043d\u0435\u043c\u0430\u043b\u043e \u043e\u043f\u044b\u0442\u043d\u044b\u0445 \u043f\u0438\u0442\u043e\u043d\u0438\u0441\u0442\u043e\u0432 \u0442\u0443\u0442 \u0436\u0435 \u043e\u0442\u0432\u0435\u0442\u044f\u0442: \u00ab\u043c\u0435\u0442\u043e\u0434 <b>__init__<\/b>\u00bb. \u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u0433\u043b\u044f\u0434\u0435\u0442\u044c\u0441\u044f \u043a \u0441\u043d\u0438\u043f\u043f\u0435\u0442\u0430\u043c \u0432\u044b\u0448\u0435, \u0432\u0441\u043a\u043e\u0440\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u043e\u0442\u0432\u0435\u0442 \u043d\u0435\u0432\u0435\u0440\u0435\u043d.<\/p>\n<p>  <\/p>\n<p><b>__init__<\/b> \u00a0\u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u0430 <b>Foo(1, y=2)<\/b>, \u043d\u0430\u043f\u0440\u043e\u0442\u0438\u0432, <strong>\u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430<\/strong>. \u041a \u0442\u043e\u043c\u0443 \u0436\u0435 <b>__init__<\/b>\u00a0\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 <b>self<\/b> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u0447\u0435\u0433\u043e \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <b>Foo(1, y=2)<\/b>. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u043e \u0447\u0451\u043c \u043c\u044b \u0438 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h1>\u041f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/h1>\n<p>  <\/p>\n<p>\u0418\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 Python \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u0442\u0430\u0434\u0438\u0439. \u041f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0448\u0430\u0433\u0430 \u0434\u0435\u043b\u0430\u0435\u0442 \u043d\u0430\u0441 \u0447\u0443\u0442\u044c \u0431\u043b\u0438\u0436\u0435 \u043a \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u044f\u0437\u044b\u043a\u0430 \u0432 \u0446\u0435\u043b\u043e\u043c. <b>Foo<\/b> \u2014 \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441, \u043d\u043e \u0432 \u041f\u0438\u0442\u043e\u043d\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u044d\u0442\u043e \u0442\u043e\u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b! \u041a\u043b\u0430\u0441\u0441\u044b, \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043c\u0435\u0442\u043e\u0434\u044b \u0438 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u2014 \u0432\u0441\u0451 \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u0438 \u0432\u0441\u044f\u043a\u0438\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0441\u0442\u0430\u0432\u0438\u0442\u0435 \u0441\u043a\u043e\u0431\u043a\u0438 \u043f\u043e\u0441\u043b\u0435 \u0438\u0445 \u0438\u043c\u0435\u043d\u0438, \u0432\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0435 \u0438\u0445 \u043c\u0435\u0442\u043e\u0434 <b>__call__<\/b>. \u0422\u0430\u043a \u0447\u0442\u043e <b>Foo(1, y=2)<\/b> \u2014 \u044d\u0442\u043e \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442 <b>Foo.__call__(1, y=2)<\/b>. \u041f\u0440\u0438\u0447\u0451\u043c \u043c\u0435\u0442\u043e\u0434 <b>__call__<\/b>\u00a0 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <b>Foo<\/b>. \u041a\u0430\u043a\u043e\u0439 \u0436\u0435 \u043a\u043b\u0430\u0441\u0441 \u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <b>Foo<\/b>?<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; Foo.__class__ &lt;class 'type'&gt;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0430\u043a \u0447\u0442\u043e \u043a\u043b\u0430\u0441\u0441 <b>Foo<\/b> \u2014 \u044d\u0442\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 <b>type<\/b> \u0438 \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 <b>__call__<\/b> \u00a0\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 <b>Foo<\/b>. \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c, \u0447\u0442\u043e \u0438\u0437 \u0441\u0435\u0431\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <b>__call__<\/b> \u00a0\u043a\u043b\u0430\u0441\u0441\u0430 <b>type<\/b>. \u041d\u0438\u0436\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 <b>C<\/b> \u0432 CPython \u0438 \u0432 PyPy. \u0415\u0441\u043b\u0438 \u043d\u0430\u0434\u043e\u0435\u0441\u0442 \u0438\u0445 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u043f\u0440\u043e\u043a\u0440\u0443\u0447\u0438\u0432\u0430\u0439\u0442\u0435 \u0447\u0443\u0442\u044c \u0434\u0430\u043b\u044c\u0448\u0435, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0439\u0442\u0438 \u0443\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e:<\/p>\n<p>  <\/p>\n<h3>CPython<\/h3>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/python\/cpython\/blob\/master\/Objects\/typeobject.c#L876\">\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a<\/a>.<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">static PyObject * type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) {     PyObject *obj;      if (type-&gt;tp_new == NULL) {         PyErr_Format(PyExc_TypeError,                      \"cannot create '%.100s' instances\",                      type-&gt;tp_name);         return NULL;     }      obj = type-&gt;tp_new(type, args, kwds);     obj = _Py_CheckFunctionResult((PyObject*)type, obj, NULL);     if (obj == NULL)         return NULL;      \/* Ugly exception: when the call was type(something),        don't call tp_init on the result. *\/     if (type == &amp;PyType_Type &amp;&amp;         PyTuple_Check(args) &amp;&amp; PyTuple_GET_SIZE(args) == 1 &amp;&amp;         (kwds == NULL ||          (PyDict_Check(kwds) &amp;&amp; PyDict_Size(kwds) == 0)))         return obj;      \/* If the returned object is not an instance of type,        it won't be initialized. *\/     if (!PyType_IsSubtype(Py_TYPE(obj), type))         return obj;      type = Py_TYPE(obj);     if (type-&gt;tp_init != NULL) {         int res = type-&gt;tp_init(obj, args, kwds);         if (res &lt; 0) {             assert(PyErr_Occurred());             Py_DECREF(obj);             obj = NULL;         }         else {             assert(!PyErr_Occurred());         }     }     return obj; }<\/code><\/pre>\n<p>  <\/p>\n<h3>PyPy<\/h3>\n<p>  <\/p>\n<p><a href=\"https:\/\/bitbucket.org\/pypy\/pypy\/src\/87c5d21350cdad5ab2ff0c0b8e2e412f0ca85ddb\/pypy\/objspace\/std\/typeobject.py?at=default&amp;amp;fileviewer=file-view-default#typeobject.py-599\">\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a<\/a>.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">def descr_call(self, space, __args__):     promote(self)     # invoke the __new__ of the type     if not we_are_jitted():         # note that the annotator will figure out that self.w_new_function         # can only be None if the newshortcut config option is not set         w_newfunc = self.w_new_function     else:         # for the JIT it is better to take the slow path because normal lookup         # is nicely optimized, but the self.w_new_function attribute is not         # known to the JIT         w_newfunc = None     if w_newfunc is None:         w_newtype, w_newdescr = self.lookup_where('__new__')         if w_newdescr is None:    # see test_crash_mro_without_object_1             raise oefmt(space.w_TypeError, \"cannot create '%N' instances\",                         self)         w_newfunc = space.get(w_newdescr, self)         if (space.config.objspace.std.newshortcut and             not we_are_jitted() and             isinstance(w_newtype, W_TypeObject)):             self.w_new_function = w_newfunc     w_newobject = space.call_obj_args(w_newfunc, self, __args__)     call_init = space.isinstance_w(w_newobject, self)      # maybe invoke the __init__ of the type     if (call_init and not (space.is_w(self, space.w_type) and         not __args__.keywords and len(__args__.arguments_w) == 1)):         w_descr = space.lookup(w_newobject, '__init__')         if w_descr is not None:    # see test_crash_mro_without_object_2             w_result = space.get_and_call_args(w_descr, w_newobject,                                                __args__)             if not space.is_w(w_result, space.w_None):                 raise oefmt(space.w_TypeError,                             \"__init__() should return None\")     return w_newobject<\/code><\/pre>\n<p>  <\/p>\n<hr>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0437\u0430\u0431\u044b\u0442\u044c \u043f\u0440\u043e \u0432\u0441\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 \u043e\u0448\u0438\u0431\u043a\u0438, \u0442\u043e \u043a\u043e\u0434\u044b \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u044b \u0442\u0430\u043a\u043e\u043c\u0443:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">def __call__(obj_type, *args, **kwargs):     obj = obj_type.__new__(*args, **kwargs)     if obj is not None and issubclass(obj, obj_type):         obj.__init__(*args, **kwargs)     return obj<\/code><\/pre>\n<p>  <\/p>\n<p><b>__new__<\/b> \u00a0\u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u043e\u0434 \u00ab\u043f\u0443\u0441\u0442\u043e\u0439\u00bb \u043e\u0431\u044a\u0435\u043a\u0442 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 <b>__init__<\/b>, \u0447\u0442\u043e\u0431\u044b \u0435\u0433\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u0434\u0432\u0435\u0434\u0451\u043c \u0438\u0442\u043e\u0433:<\/p>\n<p>  <\/p>\n<ol>\n<li><b>Foo(*args, **kwargs)<\/b> \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e <b>Foo.__call__(*args, **kwargs)<\/b>.<\/li>\n<li>\u0422\u0430\u043a \u043a\u0430\u043a \u043e\u0431\u044a\u0435\u043a\u0442 <b>Foo<\/b> \u2014 \u044d\u0442\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 <b>type<\/b>, \u0442\u043e \u0432\u044b\u0437\u043e\u0432 <b>Foo.__call__(*args, **kwargs)<\/b> \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u0435\u043d <b>type.__call__(Foo, *args, **kwargs)<\/b>.<\/li>\n<li><b>type.__call__(Foo, *args, **kwargs)<\/b> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <b>type.__new__(Foo, *args, **kwargs)<\/b>, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 <b>obj<\/b>.<\/li>\n<li><b>obj <\/b>\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435\u00a0<b>obj.__init__(*args, **kwargs)<\/b>.<\/li>\n<li>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u2014 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439<b> obj<\/b>.<\/li>\n<\/ol>\n<p>  <\/p>\n<h1>\u041a\u0430\u0441\u0442\u043e\u043c\u0438\u0437\u0430\u0446\u0438\u044f<\/h1>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0438\u043c \u043d\u0430\u0448\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 <b>__new__<\/b>. \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u043e\u0434 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0433\u043e. \u0412\u044b \u0432\u043e\u043b\u044c\u043d\u044b \u043a\u0430\u0441\u0442\u043e\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432. \u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e, \u0445\u043e\u0442\u044f <b>__new__<\/b>\u00a0 \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c, \u0432\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0442\u044c \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <b>@staticmethod<\/b>: \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 <b>__new__<\/b> \u043a\u0430\u043a \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439.<\/p>\n<p>  <\/p>\n<p>\u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f <b>__new__<\/b> \u2014 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0421\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class Singleton(object):     _instance = None          def __new__(cls, *args, **kwargs):         if cls._instance is None:             cls._instance = super().__new__(cls, *args, **kwargs)         return cls._instance<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u043e\u0433\u0434\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; s1 = Singleton() ... s2 = Singleton() ... s1 is s2 True<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e <b>__init__<\/b> \u00a0\u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <b>Singleton()<\/b>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e\u0441\u0442\u044c.<\/p>\n<p>  <\/p>\n<p>\u0414\u0440\u0443\u0433\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f <b>__new__ <\/b> \u2014 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u00a0<a href=\"https:\/\/www.safaribooksonline.com\/library\/view\/python-cookbook\/0596001673\/ch05s23.html\">\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u0411\u043e\u0440\u0433 (\u00abBorg\u00bb)<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class Borg(object):     _dict = None      def __new__(cls, *args, **kwargs):         obj = super().__new__(cls, *args, **kwargs)         if cls._dict is None:             cls._dict = obj.__dict__         else:             obj.__dict__ = cls._dict         return obj<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u043e\u0433\u0434\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; b1 = Borg() ... b2 = Borg() ... b1 is b2 False &gt;&gt;&gt; b1.x = 8 ... b2.x 8<\/code><\/pre>\n<p>  <\/p>\n<p>\u0423\u0447\u0442\u0438\u0442\u0435, \u0447\u0442\u043e \u0445\u043e\u0442\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0432\u044b\u0448\u0435 \u0438 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u044e\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f <b>__new__<\/b>, \u044d\u0442\u043e \u0435\u0449\u0451 \u043d\u0435 \u0437\u043d\u0430\u0447\u0438\u0442 \u0447\u0442\u043e \u0435\u0433\u043e \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c:<\/p>\n<p>  <\/p>\n<blockquote>\n<p><b>__new__<\/b> \u00a0 \u2014 \u043e\u0434\u043d\u0430 \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u0447\u0430\u0441\u0442\u044b\u0445 \u0436\u0435\u0440\u0442\u0432 \u0437\u043b\u043e\u0443\u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0439. \u0422\u043e, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430, \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u043b\u0443\u0447\u0448\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u043a\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e, <b>__new__ <\/b>\u2014\u00a0\u043a\u0440\u0430\u0439\u043d\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u0438 \u043c\u043e\u0449\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442.<\/p>\n<p>  <\/p>\n<p>\u2014 \u0410\u0440\u0438\u043e\u043d \u0421\u043f\u0440\u044d\u0433, <a href=\"https:\/\/concentricsky.com\/articles\/detail\/pythons-hidden-new\">\u0425\u043e\u0440\u043e\u0448\u043e \u0437\u0430\u0431\u044b\u0442\u043e\u0435 \u0441\u0442\u0430\u0440\u043e\u0435 \u0432 Python<\/a><\/p>\n<\/blockquote>\n<p>  <\/p>\n<p>\u0420\u0435\u0434\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0432 Python, \u0433\u0434\u0435 \u043b\u0443\u0447\u0448\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0431\u044b\u043b\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <b>__new__<\/b>. \u041d\u043e \u043a\u043e\u0433\u0434\u0430 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043c\u043e\u043b\u043e\u0442\u043e\u043a, \u043a\u0430\u0436\u0434\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u0430\u043a \u0433\u0432\u043e\u0437\u0434\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 <strong>\u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u043d\u043e\u0432\u043e\u0433\u043e \u043c\u043e\u0449\u043d\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0435\u0433\u043e<\/strong>.<\/p>\n<\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d() } } }t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>      <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/480022\/\"> https:\/\/habr.com\/ru\/post\/480022\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/480022\/\">\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441 <b>Foo<\/b>\u00a0:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class Foo(object):     def __init__(self, x, y=0):         self.x = x         self.y = y<\/code><\/pre>\n<p>  <\/p>\n<p>\u0427\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0435 \u0435\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442?<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">f = Foo(1, y=2)<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 <b>Foo<\/b>? \u0411\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u043d\u043e\u0432\u0438\u0447\u043a\u043e\u0432, \u0434\u0430 \u0438, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043d\u0435\u043c\u0430\u043b\u043e \u043e\u043f\u044b\u0442\u043d\u044b\u0445 \u043f\u0438\u0442\u043e\u043d\u0438\u0441\u0442\u043e\u0432 \u0442\u0443\u0442 \u0436\u0435 \u043e\u0442\u0432\u0435\u0442\u044f\u0442: \u00ab\u043c\u0435\u0442\u043e\u0434 <b>__init__<\/b>\u00bb. \u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u0433\u043b\u044f\u0434\u0435\u0442\u044c\u0441\u044f \u043a \u0441\u043d\u0438\u043f\u043f\u0435\u0442\u0430\u043c \u0432\u044b\u0448\u0435, \u0432\u0441\u043a\u043e\u0440\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u043e\u0442\u0432\u0435\u0442 \u043d\u0435\u0432\u0435\u0440\u0435\u043d.<\/p>\n<p>  <\/p>\n<p><b>__init__<\/b> \u00a0\u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u0430 <b>Foo(1, y=2)<\/b>, \u043d\u0430\u043f\u0440\u043e\u0442\u0438\u0432, <strong>\u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430<\/strong>. \u041a \u0442\u043e\u043c\u0443 \u0436\u0435 <b>__init__<\/b>\u00a0\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 <b>self<\/b> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u0447\u0435\u0433\u043e \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <b>Foo(1, y=2)<\/b>. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u043e \u0447\u0451\u043c \u043c\u044b \u0438 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-295534","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/295534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=295534"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/295534\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=295534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=295534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=295534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}