Advanced Python Concepts: Exception Handling, Collections, itertools, Decorators, Generators, and More
This tutorial covers a range of advanced Python topics—including exception handling, the collections module, itertools utilities, lambda functions, decorators, generators, threading, dunder methods, logging, and context managers—to help developers deepen their programming expertise and apply these techniques in real projects.
This article introduces several advanced Python concepts to help readers deepen their programming skills, covering exception handling, the collections module (Counter, namedtuple, OrderedDict, defaultdict, deque), itertools functions, lambda expressions, decorators, generators, process and thread basics, dunder methods, logging, and context managers.
1. Exception Handling
异常处理 is an important concept for managing errors in Python. Common built‑in exceptions such as ZeroDivisionError , ImportError , and IndexError can be caught using try and except blocks.
<code>try:
pass # code that may raise an exception
except ValueError:
pass # handle ValueError
except ZeroDivisionError:
pass # handle division by zero
else:
pass # executed if no exception occurs
finally:
pass # always executed
</code>2. collections Module
The collections module provides additional container data structures beyond the built‑in list, tuple, set, and dict.
2.1 Counter
Counts occurrences of elements in an iterable.
<code>from collections import Counter
data = [1,1,1,2,3,4,3,3,5,6,7,7,1]
count = Counter(data)
print(count) # Counter({1: 4, 2: 1, 3: 4, 4: 1, 5: 1, 6: 1, 7: 2})
print(count.most_common(3)) # [('1', 4), ('3', 4), ('2', 1)]
for i in count.elements():
print(i)
</code>2.2 namedtuple
Creates tuple subclasses with named fields.
<code>from collections import namedtuple
User = namedtuple('User', ['name', 'sex', 'age'])
user = User(name='Runoob', sex='male', age=12)
print(user) # User(name='Runoob', sex='male', age=12)
user = User._make(['RunoX', 'Male', 13])
print(user) # User(name='RunoX', sex='Male', age=13)
print(user.name, user.sex, user.age) # RunoX Male 13
user = user._replace(age=22)
print(user) # User(name='RunoX', sex='Male', age=22)
print(user._asdict()) # {'name': 'RunoX', 'sex': 'Male', 'age': 22}
</code>2.3 OrderedDict
Dictionary that remembers insertion order (now standard in Python).
<code>from collections import OrderedDict
dictt = OrderedDict()
dictt['a'] = 5
dictt['d'] = 2
dictt['c'] = 1
dictt['b'] = 3
print(dictt) # OrderedDict([('a', 5), ('d', 2), ('c', 1), ('b', 3)])
</code>2.4 defaultdict
Provides default values for missing keys.
<code># default value 0
def from collections import defaultdict
dictt = defaultdict(int)
dictt['a'] = 2
print(dictt['a']) # 2
print(dictt['b']) # 0
# equivalent using dict.get()
mydict = {'a': 1, 'b': 2}
print(mydict.get('a', 0)) # 1
print(mydict.get('c', 0)) # 0
print(mydict.get('c')) # None
</code>2.5 deque
Double‑ended queue supporting fast appends/pops from both ends.
<code>from collections import deque
queue = deque(['a', 'b', 'c'])
queue.append('d')
print(queue) # deque(['a', 'b', 'c', 'd'])
queue.appendleft('e')
print(queue) # deque(['e', 'a', 'b', 'c', 'd'])
queue.pop()
print(queue) # deque(['e', 'a', 'b', 'c'])
queue.popleft()
print(queue) # deque(['a', 'b', 'c'])
</code>3. itertools Module
The itertools module offers functions for efficient looping and combinatorial calculations.
<code>from itertools import product, permutations, combinations, combinations_with_replacement, accumulate, groupby
a = [1, 2, 3]
print(list(product(a, a)))
print(list(permutations(a)))
print(list(combinations(a, 2)))
print(list(combinations_with_replacement(a, 2)))
print(list(accumulate(a)))
print(list(groupby(a)))
</code>4. Lambda Functions
Anonymous single‑statement functions defined with the lambda keyword.
<code>even_or_odd = lambda a: a % 2 == 0
numbers = [1, 2, 3, 4, 5]
even = list(map(even_or_odd, numbers))
print(even) # [False, True, False, True, False]
</code>5. Decorators
Decorators allow adding functionality to existing functions without modifying their code.
<code>import functools
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
a, b = args
print(a * b)
result = func(*args, **kwargs)
print(a / b)
return result
return wrapper
@decorator
def add(x, y):
return x + y
result = add(5, 6)
print(result)
</code>6. Generators and Iterators
Generators use the yield keyword to produce values lazily, offering memory‑efficient iteration.
<code>def fibonacci(n):
a, b = 0, 1
for i in range(n):
a, b = b, a + b
yield a
for i in fibonacci(5):
print(i) # 1 1 2 3 5
</code>7. Processes and Threads
Threads share memory within a process, while processes run independently on separate CPU cores.
8. Dunder (Double Underscore) Methods
Dunder methods like __add__ or __mul__ enable operator overloading.
<code>num = 5
print(num * 6) # 30
print(num.__mul__(6)) # 30
a = 5
b = 6
print(a + b) # 11
print(a.__add__(b)) # 11
c = 'hello'
d = 'world'
print(c + d) # helloworld
print(c.__add__(d)) # helloworld
</code>9. Logging
The logging module provides five severity levels: Debug, Info, Warning, Error, and Critical.
A lightweight alternative is the loguru library.
10. Context Managers
Context managers simplify resource handling; the most common example is the with statement for file I/O.
<code>with open('./test.txt', 'w') as f:
f.write('Hello World!')
</code>Conclusion
The ten advanced Python topics presented here aim to enrich your skill set for work or interview preparation, encouraging you to explore decorators, yield , itertools , and other powerful features.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.