Wie konvertieren Sie ein Diktat in eine DataClass (umgekehrte Asdict)?Python

Python-Programme
Anonymous
 Wie konvertieren Sie ein Diktat in eine DataClass (umgekehrte Asdict)?

Post by Anonymous »

Das DataClasses-Modul ermöglicht es Benutzern, ein DICT aus einem DataClass-Modul wirklich bequem wie folgt zu erstellen:

Code: Select all

from dataclasses import dataclass, asdict

@dataclass
class MyDataClass:
''' description of the dataclass '''
a: int
b: int

# create instance
c = MyDataClass(100, 200)
print(c)

# turn into a dict
d = asdict(c)
print(d)
< /code>
Aber ich versuche den umgekehrten Prozess durchzuführen: dict-> DataClass.# is there a way to convert this dict to a dataclass ?
my_dict = {'a': 100, 'b': 200}
e = MyDataClass(**my_dict)
print(e)
Wie kann ich dies erreichen, ohne die DataClass vorzu definieren (falls dies möglich ist)?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post