3 ms·
I like that method of defining a named tuple much more. I understand why it's required, but having to duplicate the name of the named tuple always bothered me.
by ziml77 5y ago
I like that method of defining a named tuple much more. I understand why it's required, but having to duplicate the name of the named tuple always bothered me. The type hints are excellent to have too.
- Spivak 5y agoI don't know what you're talking about, I always confuse my coworkers by doing Pointe = namedtuple('Point'...
- wizzwizz4 5y agoThis breaks pickle, among other things.
- Spivak 5y agoIt really shouldn't. This is actually an example of pickle being broken. The class exists, there is an existing valid reference to it, and Python itself knows about it and the class itself knows its own name and namedtuple generates the right __getnewargs__. Everything is there for this to "just work" but pickle expects that every class object will have a reference to it of the same name which is kinda weird when you think about it. You can see it with this stupid little program. from collections import namedtuple import pickle def get_all_subclasses(cls): all_subclasses = [] for subclass in cls.__subclasses__(): if subclass != type: all_subclasses.append(subclass) all_subclasses.extend(get_all_subclasses(subclass)) return all_subclasses E = namedtuple('EEEEEEEEEEEEEEEEEEE', 'x') e = E(x='hello') for cls in get_all_subclasses(object): print(cls) pickle.dumps(e) You'll that the class it there and called the right thing! But pickle tries to look up a reference to it under __main__.
- wizzwizz4 5y ago> but pickle expects that every class object will have a reference to it of the same name Yeah: its qualname. The qualname, as per PEP 3155, is defined as: > a dotted path leading to the object from the module top-level so of course pickle can't cope with this information being incorrect. (How would you make it work?)
- Spivak 5y agoThere’s a difference between the classes __qualname__ and the requirement that there be reference to the class at, say, __main__.$qualname as the way for pickle to actually find the class object. This is the part that, to me, is really odd because pickle knows (in theory) the class and module name of the thing it needs to instantiate and the class objects themselves know their name and module. Up to the lack efficiency actually doing this you could just enumerate all class objects to find the one with the right name and module name. You get nested classes for free this way and you break the requirement that there be references to class objects of the same name in the module.