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.1.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.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: refvar-0.3.1.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.1.tar.gz
Algorithm Hash digest
SHA256 05985fb020074217b8886730db4fe5782b1e33e38cf1a28c97042387a035239c
MD5 042d7db88f9c1370f76647df786dc742
BLAKE2b-256 ad02fcaa4e39ede594b039861e8c9c5a6840e94ec01a170b153cadb35b4f6346

See more details on using hashes here.

File details

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

File metadata

  • Download URL: refvar-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b26ac63c24e05b67ffcbd49167d9890f44fd473e78cfcf62a6b69e19834acc34
MD5 b06faaf1d57e23fcebb2c64af5359c36
BLAKE2b-256 a68f653c273ba6a3b5dd1f328e361ab9604a1361c5a23f454877147aaf85dc17

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