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.3.tar.gz (4.9 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.3-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: refvar-0.3.3.tar.gz
  • Upload date:
  • Size: 4.9 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.3.tar.gz
Algorithm Hash digest
SHA256 87a287c5bd6b771120bd907aebed0d044fef29fc4d4ae84a995ca35d9bbb577d
MD5 5f6551cd57ba7bd19210739e616930dd
BLAKE2b-256 3229ec81ebdedbdb7a9fd51959355d46c5fb00b9ac9eaaab6f5ebbf79665f892

See more details on using hashes here.

File details

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

File metadata

  • Download URL: refvar-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 5.8 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c0c8e7090756e386fcc3f3ed083077f50b35368fc880578f1ad94c29efdd3813
MD5 a2df94d28851931fb00a85f983c6ef94
BLAKE2b-256 30fe49600760cf67c997d19858e261fd31cf2b0a7a2cd6c5e3dd116026778036

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