Skip to main content

A lightweight reactive variable for simple values in Python.

Project description

refvar

refvar is a lightweight, reactive, and efficient library for managing shared values ​​in Python.

It allows you to create reactive variables that trigger callbacks whenever their content changes—even when the value is mutable, such as lists or dictionaries.

The library is ideal for situations where multiple parts of the code need to share the same centralized variable without losing the original reference.


🚀 Features

  • Reactive variable (Ref)

  • Automatic callbacks when the value changes

  • Support for immutable and mutable values

  • Intelligent interception of mutable methods (append, pop, update, etc.)

  • Lightweight and efficient (__slots__)

  • Zero external dependencies

  • Simple and intuitive API:

  • ref(value)

  • ref(new_value) or .set()

  • ref() or .get() to get the value

  • ref(..., raw=True) to directly call the stored function

  • .bind() / .unbind() for callbacks


✨ Functionalities

  • Complete reactivity: any change triggers callbacks.

  • Compatible with mutable types: unlike previous versions.

  • Python Syntax: implements magic operators and methods.

  • Direct calls with raw=True: execute the value as a function.

  • Maximum lightweight: designed for performance and low memory usage.


🧩 What is raw=True mode?

The call:

ref(..., raw=True)

allows you to directly execute the internal value as a function, without activating the normal get/set behavior of Ref.

Examples:

1. Ref to function

log = Ref(print)

log("Hello world!", raw=True)

Output:

Hello world!

``` ```

#### 2. Ref for custom function
```python
def sum(a, b):

return a + b

f = Ref(sum)

print(f(10, 5, raw=True)) # 15

3. Keeps reactivity completely separate

The raw mode never triggers callbacks, as it does not alter ref.value, it only calls the content.

When to use raw=True?

  • When you store a function inside a Ref

  • When you want to use Ref as a functional proxy

  • When you want to avoid reactive logic and just execute something


✅ Recommended Types

The Ref class works well with all types:

Immutable:

  • str

  • int

  • float

  • bool

  • None

Mutable (fully supported in version 0.3.1):

  • list
  • dict
  • set
  • custom classes
  • objects storable in any Python structure

📦 Installation

```bashpip install refvar


---

## 🔧 Basic Example (immutable)

```python``from refvar` ... `on_change(ref, new_value):`

`print("Value changed to:", new_value)`

`x.bind(on_change)`

`x(20)` # Updates and triggers callback

`print(x())` # 20
`print(x.get())` # 20
`print(x)` # Ref(20)`


🔧 Example with (mutable) Lists

list = Ref([])`

`def on_change(ref, new_value):`

`print("List updated:", new_value)`

`list.bind(on_change)`

`list.append(1)` # triggers callback
`list.append(2)` # triggers callback
`list.pop()` # triggers callback

Output:

Updated list: [1]
Updated list: [1, 2]
Updated list: [1]


🔧 Example of Using raw=True

from refvar import Ref

def double(n):

return n * 2

f = Ref(double)

print(f(5, raw=True)) # 10

📘 License

MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

refvar-0.3.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

refvar-0.3.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file refvar-0.3.2.tar.gz.

File metadata

  • Download URL: refvar-0.3.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for refvar-0.3.2.tar.gz
Algorithm Hash digest
SHA256 04575488032b283e351dfb40d02e5245f6c8a4e2d95ec0626c2dff1541796a80
MD5 8ae8abb72903ce903cc46d92b150f1c7
BLAKE2b-256 f6d387706320f67e5b1885905446a9083b7be98a3c5d3a35a3a8e85975fb3307

See more details on using hashes here.

File details

Details for the file refvar-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: refvar-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for refvar-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 042dc9bc8956e22f79ba5f99c9cf62c4522e1cedca128cfbd1aa64badb2d9088
MD5 4543c4a2a4ae3da7b21baa73c1792b4b
BLAKE2b-256 4bb8a4a07352b544a8923bf733668311e83b41561b40a9530d7597fe3613a300

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page