MCPcopy Create free account
hub / github.com/bslatkin/effectivepython / to_bytes

Function to_bytes

example_code/item_010.py:77–82  ·  view source on GitHub ↗
(bytes_or_str)

Source from the content-addressed store, hash-verified

75
76print("Example 4")
77def to_bytes(bytes_or_str):
78 if isinstance(bytes_or_str, str):
79 value = bytes_or_str.encode("utf-8")
80 else:
81 value = bytes_or_str
82 return value # Instance of bytes
83
84print(repr(to_bytes(b"foo")))
85print(repr(to_bytes("bar")))

Callers 1

item_010.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected