Skip to content

Instantly share code, notes, and snippets.



Created May 13, 2015
What would you like to do?
rt = (lambda *args, **kw:((kw.update(dict(args))), ((type("ReturnType", (object,), dict(__slots__= [i1[0] for i1 in args] if args else list(kw.keys()) ,__getitem__=(lambda s, i:getattr(s, s.__slots__[i])), __setattr__=(lambda s, k, v: object.__setattr__(s, k ,v) if k in kw and isinstance(v, type(kw[k])) else setattr(s, getattr(s, k), None) ), __init__=(lambda s, **kw1:any(setattr(s,k1, v1) for k1, v1 in kw1.items()) or None), __repr__=(lambda s: "ReturnType({})".format(", ".join("{}={}".format(k2, repr(getattr(s, k2))) for k2 in s.__slots__) )) )))(**kw)))[1])
# Usage:
>>> test = rt(a=1, b="blah", c=open("/dev/null"))
>>> print(test)
# Or, if you need to preserve, sequence order:
>>> a = rt(("a", 5), ("b", "bla"))
>>> print(list(a))
[5, 'bla']
# Attributes of the rare named, mutable but can't be set to a different type than the creation type.
# AttributeError and TypeError are raised in the apropriate ocasions (trying to set an unexisting attribute, or trying to set an attribute to an incorrect type), but their messages are bogus.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment