Skip to content

Instantly share code, notes, and snippets.

@conqp
Created March 29, 2021 14:20
Show Gist options
  • Save conqp/6ac55087141167bb3ae17f4aaa64b99d to your computer and use it in GitHub Desktop.
Save conqp/6ac55087141167bb3ae17f4aaa64b99d to your computer and use it in GitHub Desktop.
Fun with magic dunder methods
#! /usr/bin/env python3
"""Fun with magic dunder methods."""
from pathlib import Path
class Domain(str):
"""Represents a domain name."""
def __rmatmul__(self, user: str) -> str:
"""Generates an email address from a user name."""
return f'{user}@{self}'
def main():
homedir = Path.home()
username = 'someuser'
userdir = homedir / username
print(userdir)
domain = Domain('example.com')
email_address = username @ domain
print(email_address)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment