{"id":15920629,"url":"https://github.com/vectorized/python-attribute-list","last_synced_at":"2025-10-30T05:37:11.193Z","repository":{"id":74718793,"uuid":"43377740","full_name":"Vectorized/Python-Attribute-List","owner":"Vectorized","description":"Add/set attributes to Python's built-in list.","archived":false,"fork":false,"pushed_at":"2015-09-29T16:33:18.000Z","size":136,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T20:55:14.179Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Vectorized.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-29T15:53:13.000Z","updated_at":"2024-03-03T17:23:00.000Z","dependencies_parsed_at":"2023-03-07T15:30:53.937Z","dependency_job_id":null,"html_url":"https://github.com/Vectorized/Python-Attribute-List","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vectorized%2FPython-Attribute-List","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vectorized%2FPython-Attribute-List/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vectorized%2FPython-Attribute-List/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vectorized%2FPython-Attribute-List/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Vectorized","download_url":"https://codeload.github.com/Vectorized/Python-Attribute-List/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246998228,"owners_count":20866696,"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":[],"created_at":"2024-10-06T19:40:51.666Z","updated_at":"2025-10-30T05:37:06.137Z","avatar_url":"https://github.com/Vectorized.png","language":"Python","readme":"Python Attribute List\n=====================\n\nAdd/set attributes to python lists.\n\nA google search for \"add attributes to python lists\" yields no good stackoverflow answer,\nhence the need for this.\n\nUseful for machine learning stuff where you need labeled feature vectors. \n\nThis technique can be easily adapted for other built-ins (e.g. int).\n\nThe Problem\n-----------\n\n```Python\na = [1, 2, 4, 8]\na.x = \"Hey!\" # AttributeError: 'list' object has no attribute 'x'\n```\n\nThe Solution\n------------\n\n```Python\nclass L(list):\n \n    def __new__(self, *args, **kwargs):\n        return super(L, self).__new__(self, args, kwargs)\n \n    def __init__(self, *args, **kwargs):\n        if len(args) == 1 and hasattr(args[0], '__iter__'):\n            list.__init__(self, args[0])\n        else:\n            list.__init__(self, args)\n        self.__dict__.update(kwargs)\n\n    def __call__(self, **kwargs):\n        self.__dict__.update(kwargs)\n        return self\n\na = L(1, 2, 4, 8)\na.x = \"Hey!\"\nprint a       # [1, 2, 4, 8]\nprint a.x     # \"Hey!\"\nprint len(a)  # 4\n\n# You can also do these:\na = L( 1, 2, 4, 8 , x=\"Hey!\" )                 # [1, 2, 4, 8]\na = L( 1, 2, 4, 8 )( x=\"Hey!\" )                # [1, 2, 4, 8]\na = L( [1, 2, 4, 8] , x=\"Hey!\" )               # [1, 2, 4, 8]\na = L( {1, 2, 4, 8} , x=\"Hey!\" )               # [1, 2, 4, 8]\na = L( [2 ** b for b in range(4)] , x=\"Hey!\" ) # [1, 2, 4, 8]\na = L( (2 ** b for b in range(4)) , x=\"Hey!\" ) # [1, 2, 4, 8]\na = L( 2 ** b for b in range(4) )( x=\"Hey!\" )  # [1, 2, 4, 8]\na = L( 2 )                                     # [2]\n```\n\nTags\n----\n\nadd or set attributes properties methods to list reopen class inheritance OOP subclass list elegant solution dictionary set monkey-patch short concise fast efficient easy simple compatible\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvectorized%2Fpython-attribute-list","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvectorized%2Fpython-attribute-list","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvectorized%2Fpython-attribute-list/lists"}