Code: Select all
def from_utf8(string: bytes | str) -> str:
if isinstance(string, bytes):
return string.decode("utf-8")
else:
return string  # 
Mein Verständnis ist:
Die Annotation von Typ: Bytes < /code> ist tatsächlich ein Alias für "Laufzeittypen" x: Bytes | Bytearray | majoreView [_i@meserationView] Ich habe versucht Für Typen umgekehrt: < /p>
Code: Select all
def from_utf8(string: bytes | str) -> str:
if isinstance(string, str):
return string
else:
return string.decode("utf-8")  # 
Der Fehler wird nun: < /p>
Cannot access attribute "decode" for class "memoryview[_I@memoryview]"
  Attribute "decode" is unknown
< /code>
Wenn das relevant ist: Mein Projekt verwendet Python 3.11. von from_utf8 (String)  Mobile version
 Mobile version