Skip to content

Commit c5abce8

Browse files
committed
Materials for Python bytearray
1 parent 4ecedac commit c5abce8

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

python-bytearray/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python's Bytearray: A Mutable Sequence of Bytes
2+
3+
This folder contains code associated with the Real Python tutorial [Python's Bytearray: A Mutable Sequence of Bytes](https://realpython.com/python-bytearray/).

python-bytearray/introspection.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def main():
2+
print(
3+
"Mutator methods in bytearray:",
4+
sorted(public_members(bytearray) - public_members(bytes)),
5+
)
6+
print(
7+
"Special methods in bytearray:",
8+
sorted(magic_members(bytearray) - magic_members(bytes)),
9+
)
10+
show_string_members()
11+
12+
13+
def public_members(cls):
14+
return {name for name in dir(cls) if not name.startswith("_")}
15+
16+
17+
def magic_members(cls):
18+
return {name for name in dir(cls) if name.startswith("__")}
19+
20+
21+
def show_string_members():
22+
print("String members in bytearray:")
23+
for i, name in enumerate(
24+
sorted(
25+
name
26+
for name in set(dir(bytearray)) & set(dir(str))
27+
if not name.startswith("_")
28+
),
29+
start=1,
30+
):
31+
print(f"({i:>2}) .{name}()")
32+
33+
34+
if __name__ == "__main__":
35+
main()

0 commit comments

Comments
 (0)