A lightweight reactive variable for simple values in Python.
Project description
refvar
refvar is a lightweight and reactive library for reference management in Python. It allows you to create mutable references to immutable types (such as str, int, bool) that can be shared across multiple modules and updated centrally.
The main goal is to solve the problem where importing simple variables into different files loses the link to the original value. Additionally, the library supports callbacks, allowing functions to be executed automatically whenever the value changes.
🚀 Features
-
Reactive variable (
Ref) -
Callbacks triggered automatically when the value changes
-
Extremely lightweight (< 50 lines)
-
Zero dependencies
-
Simple and intuitive API:
-
ref(value) -
ref(new_value).set()to update -
ref().get()to retrieve the content -
.bind().unbind()to call a function
✨ Features
-
Single Source of Truth: Pass variables between modules without losing the reference.
-
Reactivity: "Bind" callbacks that trigger when the value is updated.
-
Pythonic Syntax: Implements magic methods (
__call__,__eq__,__bool__,__str__) for intuitive use. -
Lightweight: Uses
__slots__for high memory efficiency.
✅ Recommended Types
Ref is recommended for simple and immutable values:
-
str -
int -
float -
bool -
None
This avoids unexpected behavior with mutable objects.
⚠️ Not recommended for:
-
list -
dict -
set -
custom classes
-
functions
-
anything mutable
If you need full reactive programming, use a framework —
refvar was specifically designed to be lightweight and simple.
📦 Installation
pip install refvar
🔧 Usage Example
Basic Example
from refvar import Ref
x = Ref(10)
def on_change(ref, new_value):
print("Value changed to:", new_value)
x.bind(on_change)
x(20) # Updates the value and triggers the callback
value = x()
print(value, type(value)) # 20 <class 'int'>
value = x.get()
print(value, type(value)) # 20 <class 'int'>
value = x
print(value, type(value)) # 20 <class 'ref.core.Ref'>
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file refvar-0.2.2.tar.gz.
File metadata
- Download URL: refvar-0.2.2.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e500647303f470e9d33918bdcc679dcddeb29cec012c343b4a6cdef8fc0fff4d
|
|
| MD5 |
4fe45fc9b52fac5d2f31096f8d4e9e95
|
|
| BLAKE2b-256 |
32af7d57367a5b864ec8bdd097e1eaa47449c994dafbdad2dda3177d9ef15725
|
File details
Details for the file refvar-0.2.2-py3-none-any.whl.
File metadata
- Download URL: refvar-0.2.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c110486e2747efd4704b6388612a1e9454b898b5344c25eee36d52264f8ee9a
|
|
| MD5 |
76ecb97c1f64c3fc0cb1c72d1ae56c5a
|
|
| BLAKE2b-256 |
5b740cf139d111b64baecdf0eea2e9c699bcfdbc630231f6d41bb68308d8e191
|