Skip to content

Instantly share code, notes, and snippets.

@ustropo
Created July 29, 2022 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ustropo/168555a15a10594632ad8e4f50b364fb to your computer and use it in GitHub Desktop.
Save ustropo/168555a15a10594632ad8e4f50b364fb to your computer and use it in GitHub Desktop.
class MyClass:
def __repr__(self) -> str:
return 'My Class Repr'
def __str__(self) -> str:
return 'My Super Class'
def __format__(self, __format_spec: str) -> str:
return 'My Formatted Class'
mc = MyClass()
print(f"Usual format call: {mc}")
## 'Usual format call: My Formatted Class'
print(f"Force str: {mc!s}")
## 'Force str: My Super Class'
print(f"Force repr: {mc!r}")
## 'Force repr: My Class Repr'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment