Jan Martinek wrote: > Zdravím, > > narazil jsem na záhadné chování pythonu. Jestliže pustím tento program > > class M: > b = 0 > def __del__(self): > M.b > > a = M() > > Nestane vůbec nic zvláštního. Ale když změním poslední řádek na > > a1 = M() > > tak to vyhodí výjimku > > Exception exceptions.AttributeError: "'NoneType' object has no attribute > 'b'" in <bound method M.__del__ of <__main__.M instance at > 0x2aaaaab50a28>> ignored > > Není to podivné, že záleží na názvu proměnné? >
Ahoj, poslal jsem to jako bugreport a zde je vyjádření vývojářů: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1500167&group_id=5470 -------------- On shutdown time, Python clears each module by replacing all module entries with None. So at some point, it does a1 = None # or a1 = None and at some other point, it does M = None Depending on the order in which these happen, M might be None (i.e. might not be class M anymore) when __del__ runs. The order in which these happen depends on the order which the names have in the module's dict. This, in turn, depends on the hash values of the strings, modulo the size of the dictionary. Apparently, "a1" gets hashed after "M", but "a" gets hashed before "M". This isn't really a bug, atleast not one that we plan to fix. The order in which modules get emptied is unspecified, and it can easily happen that things are gone when some destructor runs. Classes should be written to be resistent against this behaviour, e.g. by writing def __del__(self): self.__class__.b = 0 # name "M" might be gone already Closing this as "won't fix". ------------- Závěr je, že se nejedná o chybu, tedy alespoň ne takovou, kterou je v plánu opravit. Doporučuje se používat self.__class__. Děkuji všem za reakce. Zůstává mi ale pořád jedna nejasnost - jaktože to windowsářům tu výjimku nehází? Je tam jiné hashování+dict a je potřeba zvolit jiné názvy proměnných? Jan Martinek _______________________________________________ Python mailing list [email protected] http://www.py.cz/mailman/listinfo/python
