Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def slots_repr(self):
- """
- Provides a readable, namedtuple-like __repr__ for classes that use __slots__.
- Usage:
- >>> class A(object):
- ... __slots__ = ['b']
- ... __repr__ = slots_repr
- ...
- >>> a = A()
- >>> a.b = 5
- >>> repr(a)
- "A(b=5)"
- """
- return "{class_name}({formatted_slots})".format(
- class_name=self.__class__.__name__,
- formatted_slots=', '.join(
- "{slot}={value}".format(
- slot=slot,
- value=repr(getattr(self, slot))
- )
- for slot in self.__class__.__slots__
- if getattr(self, slot, None) is not None
- )
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement