Skip to content

Instantly share code, notes, and snippets.

@erezsh
Last active June 14, 2021 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erezsh/c516bba2c0bd9d5a00ef33a5e98d8a13 to your computer and use it in GitHub Desktop.
Save erezsh/c516bba2c0bd9d5a00ef33a5e98d8a13 to your computer and use it in GitHub Desktop.
from casts import def_cast, cast
# Define cast from tuple to list
@def_cast(auto=True)
def cast_to(i: tuple, cls: list):
return cls(i)
...
class SortedList(list):
pass
# Define cast from list to SortedList
@def_cast(auto=True)
def cast_to(l: list, cls: SortedList):
return cls(sorted(l))
# Cast tuple->list->SortedList
print(cast((4,2), SortedList)) # Prints [2, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment