Box icon indicating copy to clipboard operation
Box copied to clipboard

Internal-only Variables?

Open bwanshoom opened this issue 1 year ago • 4 comments

I apologize in advance if this is covered somewhere, but I could not find it searching the repo and the docs.

Is there a way to have a variable be internal only so it's not exported, not included in to_dict, etc.? The use case is to subclass Box and have variables internal to the class, but not included in the data.

bwanshoom avatar May 24 '24 21:05 bwanshoom

@cdgriffith would it be acceptable to make it so dunderscored variables are the ones considered "internal"? This would follow Python private name mangling conventions: https://docs.python.org/3/tutorial/classes.html#private-variables https://docs.python.org/3/reference/expressions.html#private-name-mangling

gtkacz avatar Mar 08 '25 23:03 gtkacz

I wouldn't want to broadly stroke any variables like that as internal, as I am sure someone is using it with those and would suddenly have unexpected behavior.

To bypass it at the moment you can "speak to the manager" and go above Box's head.

from box import Box

my_box = Box({"public_key": 1})

print(my_box)             # {'public_key': 1}

super(Box, my_box).__setattr__("bad_key", 33)
my_box._protected_keys.append("bad_key")

print(my_box.bad_key)     # 33
print(my_box)             # {'public_key': 1}
print(my_box.to_dict())   # {'public_key': 1}

cdgriffith avatar Mar 15 '25 01:03 cdgriffith

@cdgriffith fair enough. How should we do it then? Something like my_box.add_private_key(*dict[str, any])? They'd still be accessible as properties but wouldn't be included in exports and to_dict and etc. as the user says.

gtkacz avatar Mar 15 '25 23:03 gtkacz