Skip to main content

Solving one of Python's biggest problems. Reference types (output variables) are not implemented.

Project description

Python Reference Variables refvars

This repository contains but one simple but extremely useful Python class, Make_Reference and Reference_Instance.

This holy grail of simple python hacks, refvars provides a convenient way to work with references to objects. It allows you to set and get the value of the reference using simple methods.

Usage

To use the refvars class, simply import it into your Python script:

from refvars import Make_Reference, Reference_Instance

Then use Make_Reference to create a reference to an object:

# Create a reference
ref = Make_Reference[int](0).reference

Note that Make_Reference will run a variety of runtime AND intellisense time checks to ensure that the reference is being used correctly.

Now, you can use the Reference_Instance to get and set the value of the reference:

# To ensure that the initial value does actually change.
print(ref.get())  # 0

# If we want a function to modify the reference, we can pass the reference to the function.
def modify_reference(ref:"Reference_Instance[int]"):
    ref.set(1)

modify_reference(ref)

# Now the getting is a bit different.
print(ref.get())  # 1

Installation

To install the refvars, use pip:

pip install refvars

Confirmed versions of Python

  • 3.11 - Works perfectly.
  • 3.12 - Not tested.

License

Covered under the GNU General Public License v2.0.

V0.6 released on 12th/4/2024

Previous V0.2 didn't work.

  • Could not import the module.
  • The description did not appear on PyPi. This version hopefully fixes that.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

refvars-0.6-py3-none-any.whl (12.4 kB view hashes)

Uploaded Python 3

Supported by

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