IntFlag Composes Permission Bit Masks
Compose permission sets with IntFlag: bitwise OR grants, the in operator tests a single bit, and iterating a member lists the individual flags it contains.
Code
from enum import IntFlag, auto
# IntFlag 用位运算组合权限:一个整数表达任意权限集合
class Perm(IntFlag):
READ = auto()
WRITE = auto()
EXECUTE = auto()
ADMIN = READ | WRITE | EXECUTE
role = Perm.READ | Perm.WRITE
print("role value:", role.value, "| int eq:", int(role))
print("value 3 roundtrips:", Perm(3) == role)
# in 判断位是否开启;授权就是按位或
print("can read?", Perm.READ in role)
print("can execute?", Perm.EXECUTE in role)
role |= Perm.EXECUTE
print("granted value:", role.value, "| bits:", [p.name for p in role])
# ADMIN 覆盖全部位:包含判断一眼完成
print("admin covers all:", Perm.ADMIN == Perm.READ | Perm.WRITE | Perm.EXECUTE)
# 遍历开启的位:审计日志逐项列出权限名
print("bits:", [p.name for p in role])Output
Captured from a sandboxed run of this snippet in the build pipeline.
role value: 3 | int eq: 3
value 3 roundtrips: True
can read? True
can execute? False
granted value: 7 | bits: ['READ', 'WRITE', 'EXECUTE']
admin covers all: True
bits: ['READ', 'WRITE', 'EXECUTE']How it works
IntFlag Composes Permission Bit Masks reduces to the class Perm.
The file’s state is a handful of top-level bindings: role.
Using it
Running the file prints labeled results such as role value:, value 3 roundtrips:, can read? — each operation’s effect is visible in the output.
Interactive Tools for This Topic
Unix Permissions CalculatorConvert between octal and symbolic Unix file permissions and explain read/write/execute plus setuid, setgid, and sticky bits.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.
FAQ
- Is there an install step between copy and run?
- No. Between copying and running there is nothing left to do. Every import resolves inside the standard library — `enum`. Its state lives in module-level bindings: `role`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.