0% found this document useful (0 votes)
57 views

5 Unknown Tricks For Python Classes

The document discusses 5 tricks for Python classes: 1) Using the @property decorator to treat class attributes like properties. 2) Using the @classmethod decorator to create alternative constructors. 3) Using the Enum class from the enum module to define enumerated types. 4) Implementing the __iter__ and __next__ methods to make a class iterable. 5) Implementing the __getitem__ and __setitem__ methods to allow a class to behave like a list or dictionary.

Uploaded by

Teto Schedule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

5 Unknown Tricks For Python Classes

The document discusses 5 tricks for Python classes: 1) Using the @property decorator to treat class attributes like properties. 2) Using the @classmethod decorator to create alternative constructors. 3) Using the Enum class from the enum module to define enumerated types. 4) Implementing the __iter__ and __next__ methods to make a class iterable. 5) Implementing the __getitem__ and __setitem__ methods to allow a class to behave like a list or dictionary.

Uploaded by

Teto Schedule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Five Unknown Tricks for Python Classes | Python in Plain En... https://python.plainenglish.io/five-unknown-tricks-for-python...

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

from enum import Enum

class WeekDay(Enum):
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7

Friday

day = WeekDay.Friday

Friday

intDay = day.value # This will be 5

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...

self.l = [0 for i in range(dimension)]

def __getitem__(self, idx):


return self.l[idx-1]

def __setitem__(self, idx, data):


self.l[idx-1]=data

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

You might also like