Is there a way to create "static" (immutible) constants in Python?

Greetings!

In my remote camera robot project, I have certain variables, declared globally, that (in theory) should be “const(variable = xyz)” such that “variable” cannot be modified elsewhere in the code.

Example:
I have four global variables:
vcenter = “x”
hcenter = “y”
vposition = vcenter
hposition = hcenter

Where “x” and “y” are variables determined experimentally and represent the “centered” or resting state of both of Charlie’s head servos.

vcenter and hcenter should be immutable constant values that define the “resting” position of Charlie’s head. In other words, no matter where Charlies head is pointing, setting the horizontal and vertical positions to “hcenter” and “vcenter” will always return Charlie’s head to it’s normal looking straight ahead position.

The issue is that I, (and perhaps others), that work on my programs are supposed to set (adjust) what is the true center position for your 'bot’s head by setting these two constants at the top of the code. Unfortunately, I’m a blithering idiot and there have been times when instead of writing “hposition = hcenter” I end up transposing them “hcenter = hposition” causing wild and woolly things to happen.

What I would like to happen is that if I accidentally transpose the names, or make some other asinine mistake, the type-checker will catch it and tell me what an idiot I am rather than running one of Charlie’s servos hard against the stop, possibly breaking something.

So far, the only thing I have found is the “Finally” construct, as in:
vcenter: Finally = 74
whereupoon “vcenter” is hammered into the ground.

“Da’ bitch part” is that “Finally” only exists in Python 3.8 or later. Right now, (using RFR Buster 12/19), we’re sitting fat, dumb and happy at Python 3.7.3. - and I can’t even import it from __future__.
(I had to “escape” (\_\_) the two underscores on each side with backslashes, otherwise they were considered formatting characters.)

I’d really like to do something “standard” so that anyone with a GoPiGo can run my code.

Ideas?

An idea from https://medium.com/better-programming/does-python-have-constants-3b8249dc8b7b
(which does not like me, and I don’t like them either)

2 Likes

Another concept is to only allow getter and setters, and don’t have setters for the constants and don’t allow direct access to a variable.

Now the python interpreter will keep you honest.

1 Like

AFAIK, this applies to classes and their methods. I have not yet reached that rarified level of expertise yet.

This leaves me with two questions:

  • How do you know where a particular class definition ends, as there doesn’t seem to be a specific class end-marker like class - end_class.

  • Do you know of any documentation on Python classes that doesn’t implicitly assume a Ph.D. in Rocket Science? (Or a CS degree and a zillion years of experience.)

I already know what “classes” (in the programming sense) are, what they do, and why they can be important. I just have trouble trying to make the jump between defining program functions and program classes. To my untrained eyes, it looks like traveling all the way to Los Angeles just to pick up a gallon of milk - a lot of excess coding.

Easy… it ends where you put # END Class xyz in Coluum 1 :slight_smile:

2 Likes

Encapsulation in classes offers singleton assurance, but you don’t have to do that.

A commondata.py module can contain a module constant named “__you_cannot_use_me_X= 42”

And contain a getter function called getConstantX() that returns __you_cannot_use_me_X

Import commondata

SomeVar = commondata.getConstantX()

1 Like

Translation:
No, unless you are willing to jump through non-trivial hoops.

Rats!