Created
April 15, 2023 22:24
-
-
Save stephengruppetta/6e6afce35aa31d003dad46ab36bb4cc5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class SubstackArticle: | |
def __init__(self, number_of_words): | |
self.number_of_words = number_of_words | |
def __mul__(self, other): | |
if isinstance(other, int): | |
return SubstackArticle(self.number_of_words * other) | |
return NotImplemented | |
def __rmul__(self, other): | |
return self.__mul__(other) | |
def __repr__(self): | |
return f"SubstackArticle({self.number_of_words})" | |
my_article = SubstackArticle(1000) | |
print(my_article * 3) | |
# SubstackArticle(3000) | |
print(3 * my_article) | |
# SubstackArticle(3000) | |
print(my_article * "3") | |
# TypeError: can't multiply sequence by non-int of type 'SubstackArticle' | |
print("3" * my_article) | |
# TypeError: can't multiply sequence by non-int of type 'SubstackArticle' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment