&gt; <@U6YPB4SJX>: I notice I keep adding `__new__...
# development
a
@average-vr-56795: I notice I keep adding
__new__
overrides to datatypes to allow setting defaults arg values… Do you have any thoughts for how to pull that nicely into the superclass?
i think my first thought is that my
__new__
overrides will typically do some sort of validation as well, after doing the
super()
call -- and that this can be distinguished from default args values, which typically are determined before doing the
super()
call. so one way this can be improved is just to make a separate
classmethod
which does the sometimes-complex
super(MyType, cls).__new__(cls, *args, **kwargs)
call for you, and then making
__new__
methods is less overhead. i think another probably-better alternative would be to have one
classmethod
you can override and return a dict to set default values, and another
classmethod
you can override to perform validation of the object post-construction (where the return value is ignored (or maybe not)). i like the idea of having a declarative way to denote at least default values for arguments, and then have the `classmethod`s described above being considered a more low-level way to do so. the most obvious way i can think of is to just allow another optional element of the field tuple which contains the default value for the field. this also means the body of the
datatype
method knows about the default values, and could maybe do something with them if it wants to. i don't see a huge risk of mixing up arguments there, but one way to make that more ergonomic would be to also define a
namedtuple
class with a 1-2 letter class name to pass into the
datatype()
call just so people can use named args if they feel like it (code using these named args might look less intimidating). i agree that the proposed two `classmethod`s above (one for default args, one for validation post-construction) could be considered unnecessary when you can just override
__new__
and do both at once, but i also like the idea of explicitly splitting out default argument selection and object validation, and i think this will lead to more terse, readable code (spoilers: i did this in R a while ago and it worked but nobody was there to critique me).