Const basically makes data "Read-Only". Its an important feature to help prevent developers from changing data they were not supposed to, and to informing other developers that data marked const will not change.
Python |
Qt/Cpp |
- # Special thanks from jinsuun__ on #python irc
- from dataclasses import dataclass
- @dataclass
- class C:
- m_c: int
- isLetter : bool = False
- isNonCharacter : bool = False
- isNull : bool = False
- isNumber : bool = False
- isPrintable : bool = True
- isSpace : bool = False
- isUpper : bool = False
- n = 88
- c = C(n)
- match c:
- case C(0):
- c.isNull = True
- case C( m_c = x ) if x in range( 1, 32 ) or x == 127:
- c.isNonCharacter = True
- c.isPrintable = False
- case C(" "):
- c.isSpace = True
- case C(x) if x in range( ord("A"), ord("Z") + 1 ):
- c.isUpper = True
- c.isLetter = True
- case C(x) if x in range( ord("a"), ord("z") + 1 ):
- c.isUpper = False
- c.isLetter = True
- case C(x) if x in range( ord("0"), ord("9") + 1 ):
- c.isNumber = True
- case _:
- pass
|
- qint8 c(88);
- bool isLetter (false);
- bool isNonCharacter(false);
- bool isNull (false);
- bool isNumber (false);
- bool isPrintable (true );
- bool isSpace (false);
- bool isUpper (false);
- switch (c) {
- default: break;
- case 0 :
- isNull = true;
- [[fallthrough]];
- case 1 ... 31:
- case 127 : // Delete
- isNonCharacter = true;
- isPrintable = false;
- break;
- case ' ':
- isSpace = true;
- break;
- case 'A' ... 'Z':
- isUpper = true;
- [[fallthrough]];
- case 'a' ... 'z':
- isLetter = true;
- break;
- case '0' ... '9':
- isNumber = true;
- break;
- }
|
Read as:
|
- [ Something identified as ] 'sneed' [which may or may not already exist]
- finally [ but not really finally ] equals "feed" [ implying that sneed is a string type ].
|
- A [ new ] constant string 'sneed'
- is [ initialized as ] "feed".
|
Benefits:
|
- mypy will tell you that this variable 'shouldnt' be reassigned.
|
- Code can not be compiled if you try to overwrite sneed.
- Standard practice.
- Part of the language.
|
Drawbacks:
|
- You have to install mypy.
- Variable is not readonly.
- `sneed: Final = "feed"` can colliqually referred to as an "east const".
- Not standard practice.
- Not part of the language.
|
|