- Exception Handling
- Python Design Patterns - Anti
- Concurrency in Python
- Strings & Serialization
- Python Design Patterns - Queues
- Python Design Patterns - Sets
- Lists Data Structure
- Dictionaries
- Python Design Patterns - Iterator
- Object Oriented Concepts Implementation
- Object Oriented
- Abstract Factory
- Python Design Patterns - Flyweight
- Python Design Patterns - Template
- Python Design Patterns - Strategy
- Python Design Patterns - State
- Python Design Patterns - Observer
- Chain of Responsibility Pattern
- Python Design Patterns - Proxy
- Python Design Patterns - Decorator
- Python Design Patterns - Adapter
- Python Design Patterns - Command
- Python Design Patterns - Facade
- Python Design Patterns - Prototype
- Python Design Patterns - Builder
- Python Design Patterns - Factory
- Python Design Patterns - Singleton
- Model View Controller Pattern
- Python Design Patterns - Gist
- Introduction
- Python Design Patterns - Home
Python Design Patterns Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Design Patterns - Anti
Anti-patterns follow a strategy in opposition to predefined design patterns. The strategy includes common approaches to common problems, which can be formapzed and can be generally considered as a good development practice. Usually, anti-patterns are opposite and undesirable. Anti- patterns are certain patterns used in software development, which are considered as bad programming practices.
Important features of anti-patterns
Let us now see a few important features of anti-patterns.
Correctness
These patterns pterally break your code and make you do wrong things. Following is a simple illustration of this −
class Rectangle(object): def __init__(self, width, height): self._width = width self._height = height r = Rectangle(5, 6) # direct access of protected member print("Width: {:d}".format(r._width))
Maintainabipty
A program is said to be maintainable if it is easy to understand and modify as per the requirement. Importing module can be considered as an example of maintainabipty.
import math x = math.ceil(y) # or import multiprocessing as mp pool = mp.pool(8)
Example of anti-pattern
Following example helps in the demonstration of anti-patterns −
#Bad def filter_for_foo(l): r = [e for e in l if e.find("foo") != -1] if not check_some_critical_condition(r): return None return r res = filter_for_foo(["bar","foo","faz"]) if res is not None: #continue processing pass #Good def filter_for_foo(l): r = [e for e in l if e.find("foo") != -1] if not check_some_critical_condition(r): raise SomeException("critical condition unmet!") return r try: res = filter_for_foo(["bar","foo","faz"]) #continue processing except SomeException: i = 0 while i < 10: do_something() #we forget to increment i
Explanation
The example includes the demonstration of good and bad standards for creating a function in Python.
Advertisements