{
    "content": [
        {
            "type": "text",
            "text": "# _threading_local (pydoc)\n\n**Summary:** threadinglocal - Thread-local objects.\n\n## Section Outline\n\n- **NAME** (2 lines)\n- **MODULE REFERENCE** (8 lines)\n- **DESCRIPTION** (127 lines)\n- **CLASSES** (3 lines) — 1 subsections\n  - class local (25 lines)\n- **DATA** (2 lines)\n- **FILE** (3 lines)\n\n## Full Content\n\n### NAME\n\nthreadinglocal - Thread-local objects.\n\n### MODULE REFERENCE\n\nhttps://docs.python.org/3.10/library/threadinglocal.html\n\nThe following documentation is automatically generated from the Python\nsource files.  It may be incomplete, incorrect or include features that\nare considered implementation detail and may vary between Python\nimplementations.  When in doubt, consult the module reference at the\nlocation listed above.\n\n### DESCRIPTION\n\n(Note that this module provides a Python version of the threading.local\nclass.  Depending on the version of Python you're using, there may be a\nfaster one available.  You should always import the `local` class from\n`threading`.)\n\nThread-local objects support the management of thread-local data.\nIf you have data that you want to be local to a thread, simply create\na thread-local object and use its attributes:\n\n>>> mydata = local()\n>>> mydata.number = 42\n>>> mydata.number\n42\n\nYou can also access the local-object's dictionary:\n\n>>> mydata.dict\n{'number': 42}\n>>> mydata.dict.setdefault('widgets', [])\n[]\n>>> mydata.widgets\n[]\n\nWhat's important about thread-local objects is that their data are\nlocal to a thread. If we access the data in a different thread:\n\n>>> log = []\n>>> def f():\n...     items = sorted(mydata.dict.items())\n...     log.append(items)\n...     mydata.number = 11\n...     log.append(mydata.number)\n\n>>> import threading\n>>> thread = threading.Thread(target=f)\n>>> thread.start()\n>>> thread.join()\n>>> log\n[[], 11]\n\nwe get different data.  Furthermore, changes made in the other thread\ndon't affect data seen in this thread:\n\n>>> mydata.number\n42\n\nOf course, values you get from a local object, including a dict\nattribute, are for whatever thread was current at the time the\nattribute was read.  For that reason, you generally don't want to save\nthese values across threads, as they apply only to the thread they\ncame from.\n\nYou can create custom local objects by subclassing the local class:\n\n>>> class MyLocal(local):\n...     number = 2\n...     def init(self, /, kw):\n...         self.dict.update(kw)\n...     def squared(self):\n...         return self.number  2\n\nThis can be useful to support default values, methods and\ninitialization.  Note that if you define an init method, it will be\ncalled each time the local object is used in a separate thread.  This\nis necessary to initialize each thread's dictionary.\n\nNow if we create a local object:\n\n>>> mydata = MyLocal(color='red')\n\nNow we have a default number:\n\n>>> mydata.number\n2\n\nan initial color:\n\n>>> mydata.color\n'red'\n>>> del mydata.color\n\nAnd a method that operates on the data:\n\n>>> mydata.squared()\n4\n\nAs before, we can access the data in a separate thread:\n\n>>> log = []\n>>> thread = threading.Thread(target=f)\n>>> thread.start()\n>>> thread.join()\n>>> log\n[[('color', 'red')], 11]\n\nwithout affecting this thread's data:\n\n>>> mydata.number\n2\n>>> mydata.color\nTraceback (most recent call last):\n...\nAttributeError: 'MyLocal' object has no attribute 'color'\n\nNote that subclasses can define slots, but they are not thread\nlocal. They are shared across threads:\n\n>>> class MyLocal(local):\n...     slots = 'number'\n\n>>> mydata = MyLocal()\n>>> mydata.number = 42\n>>> mydata.color = 'red'\n\nSo, the separate thread:\n\n>>> thread = threading.Thread(target=f)\n>>> thread.start()\n>>> thread.join()\n\naffects what we see:\n\n>>> mydata.number\n11\n\n>>> del mydata\n\n### CLASSES\n\nbuiltins.object\nlocal\n\n#### class local\n\n|  local(*args, kw)\n|\n|  Methods defined here:\n|\n|  delattr(self, name)\n|      Implement delattr(self, name).\n|\n|  getattribute(self, name)\n|      Return getattr(self, name).\n|\n|  setattr(self, name, value)\n|      Implement setattr(self, name, value).\n|\n|  ----------------------------------------------------------------------\n|  Static methods defined here:\n|\n|  new(cls, /, *args, kw)\n|      Create and return a new object.  See help(type) for accurate signature.\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors defined here:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n\n### DATA\n\nall = ['local']\n\n### FILE\n\n/usr/lib/python3.10/threadinglocal.py\n\n"
        }
    ],
    "structuredContent": {
        "command": "_threading_local",
        "section": "",
        "mode": "pydoc",
        "summary": "threadinglocal - Thread-local objects.",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "MODULE REFERENCE",
                "lines": 8,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 127,
                "subsections": []
            },
            {
                "name": "CLASSES",
                "lines": 3,
                "subsections": [
                    {
                        "name": "class local",
                        "lines": 25
                    }
                ]
            },
            {
                "name": "DATA",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "FILE",
                "lines": 3,
                "subsections": []
            }
        ]
    }
}