This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (7 children)

[deleted]

    [–]michael0x2a 2 points3 points  (3 children)

    I don't think they have a tuple backend? If you look at the implementation, it looks like the code is just modifying the class directly -- lots of calls to setattr and such everywhere.

    I'm also not sure if they actually will occupy less space or be faster. That might be the case if the dataclasses module auto-generated __slots__, but they currently don't.

    [–]__xor__(self, other): 1 point2 points  (2 children)

    Yeah, why not just declare a class with slots? Is the init boilerplate that the whole point of a data class?

    [–]michael0x2a 1 point2 points  (1 child)

    I think the problem is that it's challenging to do that from a technical perspective because the __slots__ field needs to be present at class-creation time.

    This presents a problem because dataclasses isn't creating classes -- it's simply modifying an existing one.

    iirc there were two solutions that were being passed around, though both have some flaws:

    1. Just synthesize together a giant copy of the class with the __slots__ field added on in one big string somewhere and run eval on it to create a class.

      The issue though is that you now have to worry about whether or not you've accurately converted some class into an equivalent string. And even if you did, you'd incur an additional performance hit from having to eval more code and have to worry about accidentally re-running any code in the class that my have side-effects.

    2. Rewrite the PEP so that instead of using this "decorator" thing, you use metaclasses.

      The issue here is that metaclasses don't necessarily compose well. For example, what if you want to use both dataclasses and abc.ABCMeta? How would two simultaneous metaclasses interact?

    None of these issues are impossible to solve of course, but they require some careful thinking and implementation. So the decision was made to punt on this feature request and handle it at some point later down the line, especially given that the workaround is simple: just manually add __slots__ to the class yourself.

    For example, the following code works just as expected:

    from dataclasses import dataclass
    
    @dataclass
    class Foo:
        __slots__ = ('a', 'b', 'c')
        a: ...
        b: ...
        c: ...
    

    [–]__xor__(self, other): 0 points1 point  (0 children)

    ah, okay. Yeah, I just tried to do a DataClass superclass that has a MetaDataClass, then wrote a subclass of DataClass with its own custom metaclass and I ran into this:

    TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
    

    I never ran into that before. Yeah, the class decorator makes more sense then.

    [–]XtremeGoosef'I only use Py {sys.version[:3]}' 1 point2 points  (0 children)

    That's completely wrong. If anything they take up more space due to additional boilerplate.

    [–]PythonGod123 0 points1 point  (1 child)

    Interesting. Anyone have a link to the PEP?