Skip to main content

The horrors of C Static variables for Python, with Python.

Project description

staticvar

A module that adds the horrors of C Static variables to Python, with Python.

In programming, a static variable is the one allocated “statically,” which means its lifetime is throughout the program run.

Learn About Static Variables in C

Python does not provide a quick native way to declare static variables. There are some workarounds, but they don't look very nice; so I made a module that does it for you.

Currently, this module only supports integer, float, string and boolean types.

To get started, install staticvar by typing the following in your command line:

pip install staticvar



In your project, import the staticvar module as follows:

from staticvar import Static

Next, declare the name of the static variable with its value and type as the arguments:

# Syntax: VARIABLE_NAME = Static(VALUE, "TYPE")
foo = Static(3, "int")

Supported types include:

  • "int" for integer type variables
  • "float" for float type variables
  • "str" for string type variables
  • "bool" for boolean type variables

Alternatively, if no type is passed, staticvar will infer the type.

To access the value of the variable, use the get() method:

print(foo.get())

Output:

> 3

To change the value of the variable, use the set() method with the desired value as the argument:

# Syntax: VARIABLE_NAME.set(VALUE)
foo.set(4)
print(foo.get())

Output:

> 4

The set() method also returns the new set value:

print(foo.set(5))

Ouput:

> 5

To get the type of the variable, use the getType() method:

print(foo.getType())

Output:

> int

Variables set using the staticvar module are not dynamic. Trying to later assign data with different types from the originally set/inferred one will raise an error if it cannot be converted/casted:

foo.set(6.9) # A float value in an integer variable type will be casted as an integer
print(foo.get())

Output:

> 6

foo.set("Hello, mum!") # Python will fail to convert this non-numerical string into integer and will raise an error
print(foo.get())

Output:

> ValueError: invalid literal for int() with base 10: 'Hello, mum!'


An example on how to utilise static variables in a simple program

Though there are better ways to do it, we can use static variables to find the factorial of a number.

from staticvar import Static


# Using recursion and static variables to find the factorial of a number
def factorial(limit, reset = True):
	count = Static(1, "int")
	answer = Static(1) # If no type specified, staticvar will infer the type

	if reset == True:
		count.set(1)
		answer.set(1)

	if count.get() <= limit:
		answer.set(answer.get() * (count.set(count.get() + 1) - 1))
		factorial(limit, False)

	return answer.get()


user_input = eval(input("Enter a number: "))
print(factorial(user_input))

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

staticvar-0.0.6.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

staticvar-0.0.6-py3-none-any.whl (3.9 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