5 Unknown Tricks For Python Classes
5 Unknown Tricks For Python Classes
1 of 8 8/25/2021, 3:12 PM
Five Unknown Tricks for Python Classes | Python in Plain En... https://python.plainenglish.io/five-unknown-tricks-for-python...
Circle
class Circle():
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
def perimeter(self):
return 2 * 3.14 * self.radius
class Circle():
def __init__(self, radius):
self.radius = radius
self.pi = 3.14
def area(self):
return self.pi * self.radius * self.radius
def perimeter(self):
return 2 * self.pi * self.radius
c = Circle(4)
c.pi = 5
2 of 8 8/25/2021, 3:12 PM
Five Unknown Tricks for Python Classes | Python in Plain En... https://python.plainenglish.io/five-unknown-tricks-for-python...
@property
class Circle():
def __init__(self, radius):
self.radius = radius
@property
def pi(self):
return 3.14
def area(self):
return self.pi * self.radius * self.radius
def perimeter(self):
return 2 * self.pi * self.radius
__init__
@classmethod
Date
3 of 8 8/25/2021, 3:12 PM
Five Unknown Tricks for Python Classes | Python in Plain En... https://python.plainenglish.io/five-unknown-tricks-for-python...
class Date():
def __init__(self, day, month, year):
self.day = day
self.month = month
self.year = year
dd/mm/yyyy
Date
@classmethod
def fromString(cls, s):
day = int(s[:2])
month = int(s[3:5])
year = int(s[6:])
return cls(day, month, year) # Return a new object
Date fromString
d = Date.fromString("21/07/2020")
print(d.day, d.month, d.year)
Output:
21 7 2020
4 of 8 8/25/2021, 3:12 PM
Five Unknown Tricks for Python Classes | Python in Plain En... https://python.plainenglish.io/five-unknown-tricks-for-python...
enum
Enum
class WeekDay(Enum):
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
Friday
day = WeekDay.Friday
Friday
range
5 of 8 8/25/2021, 3:12 PM
Five Unknown Tricks for Python Classes | Python in Plain En... https://python.plainenglish.io/five-unknown-tricks-for-python...
__next__ __iter__
__iter__ self
__next__
class Backward():
def __init__(self, data):
self.data = data # The list we want to iterate
self.index = len(self.data)
def __iter__(self):
return self
def __next__(self):
if(self.index == 0):
raise StopIteration
else:
self.index -= 1
return self.data[self.index]
__next__ StopIteration
Backward
6 of 8 8/25/2021, 3:12 PM
Five Unknown Tricks for Python Classes | Python in Plain En... https://python.plainenglish.io/five-unknown-tricks-for-python...
bw = Backward([1,2,3,4,5])
for elem in bw:
print(elem)
5
4
3
2
1
__getitem__ __setitem__
x = l[idx] x = l.__getitem__(idx)
l[idx] = x l.__setitem__(idx, x)
class MyList():
def __init__(self, dimension):
7 of 8 8/25/2021, 3:12 PM
Five Unknown Tricks for Python Classes | Python in Plain En... https://python.plainenglish.io/five-unknown-tricks-for-python...
ml = MyList(5)
ml[1] = 50 # Set the first element of ml to 50
ml[2] = 100 # Set the second element of ml to 100
x = ml[1] # x is now 50
@property
@classmethod
Enum
__next__
__setitem__
__getitem__
8 of 8 8/25/2021, 3:12 PM