[Python-de] Re: dataclass: Unterschied zu automatischer Generierung

[email protected]
Newsgroups gmane.comp.python.general.german
Message-ID <[email protected]>
Eine Konsequenz scheint zu sein, dass ich eine automatisch erzeugte 
dataclass nicht pickeln kann. Ich bekomme den Fehler 
"_pickle.PicklingError: Can't pickle <class 'types.X'>: attribute lookup 
X on types failed"

Wenn ich aber den Modulnamen ändere
x.__module__ = '__main__'
geht es auch mit Pickle.

Am 13.05.2022 10:06 schrieb [email protected]:
> Hallo,
> 
> meine Frage bezieht sich auf dieses StackOverflow-Posting:
> https://stackoverflow.com/q/72217470/4865723
> 
> Dort geht es darum, wie man aus den Keys eines dict automatisch eine
> dataclass erzeugen könnte. Erst einmal erscheint es einfach:
>>>> import dataclasses
>>>> d = {'a': 3, 'b': 7}
>>>> X = dataclasses.make_dataclass('X', d)
> 
> Aber es gibt hier im Ergebnis einen Unterschied zu einer regulär
> erzeugten dataclass.
>>>> X
> <class 'types.X'>
> 
> Regulär erzeugt
>>>> @dataclasses.dataclass
> ... class X:
> ...     a: int
> ...     b: int
> ...
>>>> X
> <class '__main__.X'>
> 
> In einem Fall "types.X" und im anderen "__main__.X". Warum ist das so
> und ist dieser Unterschied von Relevanz?
> 
> SG
> Christian
> _______________________________________________
> python-de Mailingliste -- [email protected]
> Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an
> [email protected]
> https://mail.python.org/mailman3/lists/python-de.python.org/
> Mitgliedsadresse: [email protected]
_______________________________________________
python-de Mailingliste -- [email protected]
Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an [email protected]
https://mail.python.org/mailman3/lists/python-de.python.org/
Mitgliedsadresse: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.