Created
January 8, 2021 08:05
-
-
Save jangia/7f417094eac965a9cf819b6c3c49b296 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 Stick: | |
def __init__(self, color, length, weight): | |
self.color = color | |
self.length = length | |
self.weight = weight | |
stick = Stick('yellow', 1, 0.324) | |
class Stick: | |
def __init__(self, color, length, weight): | |
self.color = color | |
self.length = length | |
self.weight = weight | |
def __repr__(self): | |
return f'Stick({self.color!r}, {self.length}, {self.weight})' | |
def __str__(self): | |
return f'{self.color} stick' | |
stick = Stick('yellow', 1, 0.324) | |
print(str(stick)) | |
# yellow stick | |
print(f'I have a {stick}') | |
# I have a yellow stick | |
print(repr(stick)) | |
# Stick('yellow', 1, 0.324) | |
class Stick: | |
def __init__(self, color, length, weight): | |
self.color = color | |
self.length = length | |
self.weight = weight | |
def __eq__(self, other): | |
return (self.color, self.length, self.weight) == (other.color, other.length, other.weight) | |
def __ne__(self, other): | |
return not self.__eq__(other) | |
def __lt__(self, other): | |
return self.length < other.length | |
def __le__(self, other): | |
return self.length <= other.length | |
def __gt__(self, other): | |
return self.length < other.length | |
def __ge__(self, other): | |
return self.length <= other.length | |
def __add__(self, other): | |
return Stick(f'{self.color}/{other.color}', self.length + other.length, self.weight + other.weight) | |
def __repr__(self): | |
return f'Stick({self.color!r}, {self.length}, {self.weight})' | |
stick1 = Stick('yellow', 1, 0.324) | |
stick2 = Stick('yellow', 1, 0.324) | |
stick3 = Stick('blue', 0.75, 0.454) | |
print(stick1 == stick2) | |
# True | |
print(stick1 == stick3) | |
# False | |
print(stick1 > stick3) | |
# False | |
print(sorted([stick2, stick3, stick1])) | |
# [Stick('blue', 0.75, 0.454), Stick('yellow', 1, 0.324), Stick('yellow', 1, 0.324)] | |
print(stick1 + stick2 + stick3) | |
# Stick('yellow/yellow/blue', 2.75, 1.102) | |
class Stick: | |
def __init__(self, color, length, weight): | |
self.color = color | |
self.length = length | |
self.weight = weight | |
def __call__(self, *args, **kwargs): | |
print('Spank, spank, spank, ...') | |
stick1 = Stick('yellow', 1, 0.324) | |
stick1() | |
# Spank, spank, spank, ... | |
class FirstNOddNumbers: | |
def __init__(self, n): | |
self.n = n | |
self.count = 0 | |
self.number = 1 | |
def __iter__(self): | |
return self | |
def __next__(self): | |
if self.count < self.n: | |
cur, self.number = self.number, self.number + 2 | |
self.count += 1 | |
return cur | |
raise StopIteration() | |
class Stick: | |
def __init__(self, color, length, weight): | |
self.color = color | |
self.length = length | |
self.weight = weight | |
def __getitem__(self, item): | |
return getattr(self, item) | |
def __setitem__(self, key, value): | |
setattr(self, key, value) | |
stick1 = Stick('yellow', 1, 0.324) | |
stick1['color'] = 'green' | |
print(stick1['color']) | |
# green |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment