From d1ea1f731727576c62be48209be0709fb1a97d97 Mon Sep 17 00:00:00 2001 From: Cacahuete Lenovo Ubuntu Date: Sun, 16 Sep 2018 15:49:12 +0200 Subject: [PATCH] Code cleaning: StrType class now derives from built-in str so that __hash__ and __eq__ are not to be definded --- nested_mapping.py | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/nested_mapping.py b/nested_mapping.py index c6b7bed..c3ac3de 100644 --- a/nested_mapping.py +++ b/nested_mapping.py @@ -8,30 +8,17 @@ def unique(seq): return type(seq)(uniques) -class StrType: +class StrType(str): """Helper class to print and compare object types. - __eq__ implemented to allow for StrType enumaration - __hash__ implemented for compatibility with set objects - __repr__ and __str__ to print type(StrType.obj) as desired. + Provide the object whose type you want to print. """ - def __init__(self, obj): - """Provide the object whose type you want to print.""" - self.obj = obj - - def __eq__(self, other): - if not isinstance(other, StrType): - return False - return type(self.obj) == type(other.obj) - - def __hash__(self): - return hash(str(self)) + self.value = type(obj).__name__ def __str__(self): - return f"<{type(self.obj).__name__}>" + return f"<{self.value}>" - def __repr__(self): - return self.__str__() + __repr__ = __str__ def get_structure(data):