{"id":16908144,"url":"https://github.com/cjrh/autoslot","last_synced_at":"2025-04-10T01:11:54.919Z","repository":{"id":45788397,"uuid":"105368160","full_name":"cjrh/autoslot","owner":"cjrh","description":"Automatic __slots__ for your Python classes","archived":false,"fork":false,"pushed_at":"2024-12-06T14:42:41.000Z","size":68,"stargazers_count":62,"open_issues_count":1,"forks_count":5,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-10T01:11:51.262Z","etag":null,"topics":["efficiency","metaclass","metaprogramming","python","python-3","slots"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cjrh.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["cjrh"]}},"created_at":"2017-09-30T12:24:04.000Z","updated_at":"2025-04-04T06:08:30.000Z","dependencies_parsed_at":"2024-10-28T13:15:36.406Z","dependency_job_id":"decb2508-8e31-489e-82a5-fcc2c3ea8bd4","html_url":"https://github.com/cjrh/autoslot","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjrh%2Fautoslot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjrh%2Fautoslot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjrh%2Fautoslot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjrh%2Fautoslot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cjrh","download_url":"https://codeload.github.com/cjrh/autoslot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137888,"owners_count":21053775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["efficiency","metaclass","metaprogramming","python","python-3","slots"],"created_at":"2024-10-13T18:50:22.157Z","updated_at":"2025-04-10T01:11:54.887Z","avatar_url":"https://github.com/cjrh.png","language":"Python","funding_links":["https://github.com/sponsors/cjrh"],"categories":[],"sub_categories":[],"readme":".. image:: https://img.shields.io/badge/stdlib--only-yes-green.svg\n    :target: https://img.shields.io/badge/stdlib--only-yes-green.svg\n\n.. image:: https://coveralls.io/repos/github/cjrh/autoslot/badge.svg?branch=master\n    :target: https://coveralls.io/github/cjrh/autoslot?branch=master\n\n.. image:: https://img.shields.io/pypi/pyversions/autoslot.svg\n    :target: https://pypi.python.org/pypi/autoslot\n\n.. image:: https://img.shields.io/github/tag/cjrh/autoslot.svg\n    :target: https://img.shields.io/github/tag/cjrh/autoslot.svg\n\n.. image:: https://img.shields.io/badge/install-pip%20install%20autoslot-ff69b4.svg\n    :target: https://img.shields.io/badge/install-pip%20install%20autoslot-ff69b4.svg\n\n.. image:: https://img.shields.io/pypi/v/autoslot.svg\n    :target: https://img.shields.io/pypi/v/autoslot.svg\n\n.. image:: https://img.shields.io/badge/calver-YYYY.MM.MINOR-22bfda.svg\n    :target: http://calver.org/\n\nautoslot\n========\n\nAutomatic \"__slots__\".\n\nDemo\n----\n\n.. code-block:: python\n\n   from autoslot import Slots\n\n   class Compact(Slots):\n       def __init__(self, a, b):\n           self.x = a\n           self.y = b\n\nThis produces *exactly* the same class as if you had done:\n\n.. code-block:: python\n\n   class Compact:\n       __slots__ = {'x', 'y'}\n       def __init__(self, a, b):\n           self.x = a\n           self.y = b\n\nSimply: the code inside ``__init__()`` is scanned to find all assignments\nto attributes on ``self``, and these are added as ``__slots__``.\n\nThe benefit of using ``autoslot.Slots`` over a manual slots declaration is\nthat you can modify the\ncode inside the ``__init__()`` method to add more attributes, and those\nchanges will *automatically* be reflected in the ``__slots__`` definition.\n\nYou can also have the best of both worlds: slots for fields you expect,\n**as well as** a ``__dict__`` for those you don't:\n\n.. code-block:: python\n\n   from autoslot import SlotsPlusDict\n\n   class SemiCompact(SlotsPlusDict):\n       def __init__(self, a, b):\n           self.x = a\n           self.y = b\n\n   inst = SemiCompact(1, 2)\n   inst.z = 123  # \u003c-- This won't fail!\n\nAttributes ``x`` and ``y`` will be stored in slots, while all other\ndynamically-assigned attributes will go into the usual ``__dict__`` instance\ninside the class.  If most of your class's attributes appear in the ``__init__()``\nmethod (these will become slots), then the space bloat caused by dictionary\nhash-table expansion will be contained to only the dynamically-assigned\nattributes.\n\nHow does it work?\n-----------------\n\nSee for yourself! The code is tiny.\n\nIn words: the metaclass finds the ``__init__()`` method, if present, and\naccesses its bytecode. It looks for all assignments to attributes of\n``self``, and considers those to be desired ``__slots__`` entries. Then the\nmetaclass injects ``__slots__`` into the namespace of the class definition\nand thereafter allows class creation to proceed as normal.\n\nWeakref\n-------\n\nWhen ``__slots__`` are used, weak references (e.g. using the weakref_\nstandard library module) won't work. If you need weak references, just\nset it up on a new ``__slots__`` class variable as you would normally\ndo without using ``autoslot``:\n\n.. code-block:: python\n\n   from autoslot import Slots\n\n   class Compact(Slots):\n       __slots__ = ['__weakref__']\n\n       def __init__(self, a, b):\n           self.x = a\n           self.y = b\n\nEverything else will still work, and instances of ``Compact`` will now\nalso play nicely with the weakref_ module.\n\n.. _weakref: https://docs.python.org/3/library/weakref.html?highlight=weakref#module-weakref\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcjrh%2Fautoslot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcjrh%2Fautoslot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcjrh%2Fautoslot/lists"}