assign-overload


Nameassign-overload JSON
Version 1.0.1 PyPI version JSON
download
home_pageNone
Summaryoverloading assignment operator everywhere
upload_time2025-10-09 18:36:41
maintainerNone
docs_urlNone
authorNone
requires_pythonNone
licenseNone
keywords assign assignment magic method operator overloading
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # assign-overload
This library makes it possible to overload assignment (=) operator. Inspired by [assign](https://github.com/RyanKung/assign).
Along with other things, main difference from that library is [assign](https://github.com/RyanKung/assign) calls the overloaded operator of the right hand side while this library calls of the left hand side.

# Installation
```pip install assign-overload```

# How To Use
First you need to overload the assignment operator using the function ```_assign_```:
```python
class T:        
    def _assign_(self, value, *annotation):
        print(f"called with {value}")
        return self
```
_value_ is the value of right hand side. If the assignment is annotated, _annotation_ will take a single argument carrying the annotation. Return value specifies the value of left hand side.

Next, in order to make this method automatically called you should call assign_overload.**patch_and_reload_module**():
```python
import assign_overload

class T:        
    def _assign_(self, value, *annotation):
        print(f"called with {value}")
        return self


def main():
    a = T()
    a = 10 # a will keep holding the T object


if assign_overload.patch_and_reload_module():
    if __name__ == "__main__":
        main()
```
This function will find and modify the current module's source code, replacing right hand side of assignments with calls to ```_assign_```, then execute the modified code. 
Once called, this function will introduce a new global variable to the current module: ```modified_source```. 
This variable will be accessible while both executing the original module and modified module. 
It represents the source code of the modified module.
Since module's ```__dict__``` attribute is passed as the globals parameter of ```exec```, names created in the modified module will be reflected on the original module along with their values, creating a reloading effect. 
Return value of this function specifies which module we are currently executing. True means modified module and False means original module.
Any code outside the first if block will be executed twice. 
Codes before this function call will first be executed while executing the original module and second while executing the modified module.
Codes after the first if block will first be executed while executing the modified module and second while executing the original module. 
This is especially bad because if a piece of code last executed while executing the original module, any function or class definition will have its original definition, not the modified definition they should have.
Codes inside the first if block will only be executed while executing the modified module.
You can wrap all your module code including functions and classes inside a single if block like this:
```python
import assign_overload

if assign_overload.patch_and_reload_module():
    class T:        
        def _assign_(self, value, *annotation):
            print(f"called with {value}")
            return self


    def main():
        a = T()
        a = 10 # a will keep holding the T object


    if __name__ == "__main__":
        main()
```
But that doesn't look nice. 
Functions, classes and constant definitions are most probably okay to be executed twice but actual code should be executed once.
Thus, the location in the first example is the best location for calling this function.
This function should be called even if this module isn't the ```__main__``` module because function and class definitions should be modified even if no real code is ought to be executed.
This function should be called once. Consecutive calls doesn't modify and execute the source code again. 
Lastly, this function will introduce 2 other global varriables to the current module: ```patched``` and ```executing_patch```.
These variables are internally used by the library and they are documented here only to prevent the user from changing them.

# Limitations
Unpacking during assignment and the walrus operator are not supported. 
If you attempt to call assign_overload.patch_and_reload_module() from a module using these features, you will face an error.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "assign-overload",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "assign, assignment, magic, method, operator, overloading",
    "author": null,
    "author_email": "pyhacks <enginyildirim111@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/4c/ec/559122bf380bbe146d719d8d466f9432fe6c64d5ff5117486c73befe7d30/assign_overload-1.0.1.tar.gz",
    "platform": null,
    "description": "# assign-overload\nThis library makes it possible to overload assignment (=) operator. Inspired by [assign](https://github.com/RyanKung/assign).\nAlong with other things, main difference from that library is [assign](https://github.com/RyanKung/assign) calls the overloaded operator of the right hand side while this library calls of the left hand side.\n\n# Installation\n```pip install assign-overload```\n\n# How To Use\nFirst you need to overload the assignment operator using the function ```_assign_```:\n```python\nclass T:        \n    def _assign_(self, value, *annotation):\n        print(f\"called with {value}\")\n        return self\n```\n_value_ is the value of right hand side. If the assignment is annotated, _annotation_ will take a single argument carrying the annotation. Return value specifies the value of left hand side.\n\nNext, in order to make this method automatically called you should call assign_overload.**patch_and_reload_module**():\n```python\nimport assign_overload\n\nclass T:        \n    def _assign_(self, value, *annotation):\n        print(f\"called with {value}\")\n        return self\n\n\ndef main():\n    a = T()\n    a = 10 # a will keep holding the T object\n\n\nif assign_overload.patch_and_reload_module():\n    if __name__ == \"__main__\":\n        main()\n```\nThis function will find and modify the current module's source code, replacing right hand side of assignments with calls to ```_assign_```, then execute the modified code. \nOnce called, this function will introduce a new global variable to the current module: ```modified_source```. \nThis variable will be accessible while both executing the original module and modified module. \nIt represents the source code of the modified module.\nSince module's ```__dict__``` attribute is passed as the globals parameter of ```exec```, names created in the modified module will be reflected on the original module along with their values, creating a reloading effect. \nReturn value of this function specifies which module we are currently executing. True means modified module and False means original module.\nAny code outside the first if block will be executed twice. \nCodes before this function call will first be executed while executing the original module and second while executing the modified module.\nCodes after the first if block will first be executed while executing the modified module and second while executing the original module. \nThis is especially bad because if a piece of code last executed while executing the original module, any function or class definition will have its original definition, not the modified definition they should have.\nCodes inside the first if block will only be executed while executing the modified module.\nYou can wrap all your module code including functions and classes inside a single if block like this:\n```python\nimport assign_overload\n\nif assign_overload.patch_and_reload_module():\n    class T:        \n        def _assign_(self, value, *annotation):\n            print(f\"called with {value}\")\n            return self\n\n\n    def main():\n        a = T()\n        a = 10 # a will keep holding the T object\n\n\n    if __name__ == \"__main__\":\n        main()\n```\nBut that doesn't look nice. \nFunctions, classes and constant definitions are most probably okay to be executed twice but actual code should be executed once.\nThus, the location in the first example is the best location for calling this function.\nThis function should be called even if this module isn't the ```__main__``` module because function and class definitions should be modified even if no real code is ought to be executed.\nThis function should be called once. Consecutive calls doesn't modify and execute the source code again. \nLastly, this function will introduce 2 other global varriables to the current module: ```patched``` and ```executing_patch```.\nThese variables are internally used by the library and they are documented here only to prevent the user from changing them.\n\n# Limitations\nUnpacking during assignment and the walrus operator are not supported. \nIf you attempt to call assign_overload.patch_and_reload_module() from a module using these features, you will face an error.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "overloading assignment operator everywhere",
    "version": "1.0.1",
    "project_urls": {
        "Homepage": "https://github.com/pyhacks/assign-overload"
    },
    "split_keywords": [
        "assign",
        " assignment",
        " magic",
        " method",
        " operator",
        " overloading"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "acf01f9638149c1a8aafc09d29de615590418d691b53973b525559b3d539eaec",
                "md5": "16010887da4f872f846ba2eb547f7185",
                "sha256": "f91367a8f2694e8dfc502478aabfeb735ba49a173f24b9ff64a4dea275378b1f"
            },
            "downloads": -1,
            "filename": "assign_overload-1.0.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "16010887da4f872f846ba2eb547f7185",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 4965,
            "upload_time": "2025-10-09T18:36:40",
            "upload_time_iso_8601": "2025-10-09T18:36:40.805195Z",
            "url": "https://files.pythonhosted.org/packages/ac/f0/1f9638149c1a8aafc09d29de615590418d691b53973b525559b3d539eaec/assign_overload-1.0.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "4cec559122bf380bbe146d719d8d466f9432fe6c64d5ff5117486c73befe7d30",
                "md5": "7f710a17565a7de83975128337fb7e27",
                "sha256": "e4b0659a4df451549b765f77726de200df1bd8c7580dfb3287bea2733c773d57"
            },
            "downloads": -1,
            "filename": "assign_overload-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "7f710a17565a7de83975128337fb7e27",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 3920,
            "upload_time": "2025-10-09T18:36:41",
            "upload_time_iso_8601": "2025-10-09T18:36:41.942774Z",
            "url": "https://files.pythonhosted.org/packages/4c/ec/559122bf380bbe146d719d8d466f9432fe6c64d5ff5117486c73befe7d30/assign_overload-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-09 18:36:41",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pyhacks",
    "github_project": "assign-overload",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "assign-overload"
}
        
Elapsed time: 0.75890s