You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It can be instantiated like this: Status.Error("This and that is wrong etc.", code="mismatched-something"). However, instantiating it like Status.Error() should result in Status(weight=6, message="", code=""), i.e. I want to avoid None objects. Python unfortunately makes it more tedious than it should be to give out non-None default arguments.
@hynek noted that I may want to use default="" and use attr.NOTHING as the default argument in the constructors, however:
In [1]: import attr
...: @attr.s
...: class Status:
...: a = attr.ib(default="")
...: @classmethod
...: def aaa(cls, a=attr.NOTHING):
...: return cls(a)
...:
In [2]: Status.aaa()
Out[2]: Status(a=NOTHING)
In [3]: Status.aaa("")
Out[3]: Status(a='')
In [4]: import attr
...: @attr.s
...: class Status:
...: a = attr.ib(default="")
...: @classmethod
...: def aaa(cls, a=None):
...: return cls(a=a or attr.NOTHING)
In [5]: Status.aaa()
Out[5]: Status(a=NOTHING)
The text was updated successfully, but these errors were encountered:
I guess what could be done is some kind of attr.converters.default_if_none(default=NOTHING, factory=None) so you could write attr.converters.default_if_none(default="") or attr.converters.default_if_none(factory=list). I think I had uses for that too before. 🤔
(continuing from #170 which I don't want to derail)
I have a class like this:
It can be instantiated like this:
Status.Error("This and that is wrong etc.", code="mismatched-something")
. However, instantiating it likeStatus.Error()
should result inStatus(weight=6, message="", code="")
, i.e. I want to avoid None objects. Python unfortunately makes it more tedious than it should be to give out non-None default arguments.@hynek noted that I may want to use
default=""
and useattr.NOTHING
as the default argument in the constructors, however:The text was updated successfully, but these errors were encountered: