Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constants class? #15

Open
clarkedavida opened this issue Dec 10, 2022 · 1 comment
Open

constants class? #15

clarkedavida opened this issue Dec 10, 2022 · 1 comment

Comments

@clarkedavida
Copy link
Collaborator

If you are not careful with lists, you can accidentally change variables without meaning to through shallow copies. Is there a way to implement a constants class that can't be accidentally modified?

Maybe something like this: https://discuss.dizzycoding.com/can-i-prevent-modifying-an-object-in-python/

@luhuhis
Copy link
Collaborator

luhuhis commented Dec 10, 2022

I do that usually like this:

from typing import NamedTuple
class test(NamedTuple):
    blub: int
    bla: float

this_test = test(4, 3.2)
this_test.blub = 5  # this will fail, because it's immutable.

This can be used to pass one immutable "parameter object" to a function, instead of passing lots of single parameter which are then also mutable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants