rope
rope copied to clipboard
The Inline Method removes an overridden method
The Inline Method removes an overridden method that changes the type of the stored objects.
Steps to reproduce the behavior:
- Code before refactoring:
class Word(str):
def __new__(cls, value):
print(f"criando Word({value!r})")
return super().__new__(cls, value)
class WordList(list):
def append(self, obj):
print(f"append: recebi {obj!r} (type={type(obj).__name__})")
if isinstance(obj, str) and not isinstance(obj, Word):
obj = Word(obj)
super().append(obj)
def extend(self, iterable):
print("extend (custom): chamando append para cada elemento")
for e in iterable:
self.append(e)
wl = WordList()
wl.extend(["ola", "mundo"])
print("conteúdo wl:", wl)
print("types wl:", [type(x).__name__ for x in wl])
-
Apply the inline method to
WordList.extend -
Code after refactoring:
class Word(str):
def __new__(cls, value):
print(f"criando Word({value!r})")
return super().__new__(cls, value)
class WordList(list):
def append(self, obj):
print(f"append: recebi {obj!r} (type={type(obj).__name__})")
if isinstance(obj, str) and not isinstance(obj, Word):
obj = Word(obj)
super().append(obj)
wl = WordList()
wl.extend(["ola", "mundo"])
print("conteúdo wl:", wl)
print("types wl:", [type(x).__name__ for x in wl])