Skip to main content

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

  • Safe by design — supports only simple types


✨ 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

refvar-0.2.1.tar.gz (3.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.2.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: refvar-0.2.1.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

Hashes for refvar-0.2.1.tar.gz
Algorithm Hash digest
SHA256 118ee30a44c92aa1c93dd61a3011d857e2796d2bbdca430d1717129128902fe1
MD5 472a373f426880afd9c82b7552530105
BLAKE2b-256 a0ba3b5996da43d503872594d253f27fcc20791800cc9c4289fedf06bc7d76d4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: refvar-0.2.1-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

Hashes for refvar-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5f2da74bfdfc1463c3d223102d119a23392266f6369f2137d5d7fc2bc5521cdf
MD5 c99a078b11395e1e8678ed82c95ba7e9
BLAKE2b-256 f385d53e80965ca63b0449e4f570a946eac53f382230a1860dd05a75d66f32e9

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