Last active
April 16, 2022 19:24
-
-
Save axil/c2553f44fbba406cb63320c67bbc7d4a to your computer and use it in GitHub Desktop.
subclass_tuple
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
class Point(tuple): | |
def __new__(cls, x, y): | |
return super().__new__(cls, (x, y)) | |
@property | |
def x(self): | |
return self[0] | |
@property | |
def y(self): | |
return self[1] | |
def __repr__(self): | |
return f'Point({self.x}, {self.y})' | |
>>> points = [Point(1,2), Point(2,3), Point(1,2)] | |
>>> Counter(points) | |
Counter({Point(1, 2): 2, Point(2, 3): 1}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment