https://github.com/astropenguin/morecopy
Copy even immutable objects as much as possible
https://github.com/astropenguin/morecopy
copy deepcopy python
Last synced: about 1 year ago
JSON representation
Copy even immutable objects as much as possible
- Host: GitHub
- URL: https://github.com/astropenguin/morecopy
- Owner: astropenguin
- License: mit
- Created: 2021-10-23T07:10:07.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-10-19T12:12:05.000Z (over 2 years ago)
- Last Synced: 2025-03-21T04:01:53.089Z (over 1 year ago)
- Topics: copy, deepcopy, python
- Language: Python
- Homepage:
- Size: 63.5 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Citation: CITATION.cff
Awesome Lists containing this project
README
# morecopy
[](https://pypi.org/project/morecopy/)
[](https://pypi.org/project/morecopy/)
[](https://pepy.tech/project/morecopy)
[](https://doi.org/10.5281/zenodo.5594444)
[](https://github.com/astropenguin/morecopy/actions)
Copy even immutable objects as much as possible
## Overview
morecopy is a Python package that enables copy of immutable objects so that a copied object is equivalent but not identical to the original:
```python
from morecopy import copy
original = 1234567890
copied = copy(original)
original == copied # -> True
original is copied # -> False
```
> **Note**
> In general, there is no need to copy immutable objects, so this package may not be necessary in most cases.
> Also, some objects may not be copied even with this package:
> In CPython, for example, integers from -5 to 256 are always uncopied for optimization.
## Installation
```shell
$ pip install morecopy
```
## Supported immutable types
The following types are supported.
For mutable types (e.g. `list`) or unsupported immutable types (e.g. `bool`, `NoneType`), `morecopy.copy` and `morecopy.deepcopy` are equivalent to `copy.copy` and `copy.deepcopy`, respectively.
Type | `morecopy.copy` | `morecopy.deepcopy`
--- | --- | ---
`int` | yes | n/a
`float` | yes | n/a
`complex` | yes | n/a
`str` | yes | n/a
`bytes` | yes | n/a
`tuple` | yes | n/a
`range` | yes | n/a
`slice` | yes | n/a
`frozenset` | yes | n/a
`FunctionType` | yes | n/a
`LambdaType` | yes | n/a
## Custom immutable copier
Users can add a custom copy function (copier) for a type.
For example, the following code defines copy of integer by creating a copy function and registering it by the `copier_for` decorator.
```python
from morecopy import copier_for
@copier_for(int)
def copy_int(integer: int) -> int:
return eval(repr(integer))
```