Created
March 29, 2021 14:20
-
-
Save conqp/6ac55087141167bb3ae17f4aaa64b99d to your computer and use it in GitHub Desktop.
Fun with magic dunder methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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