Skip to content

Commit 6203973

Browse files
committed
Iterator started
1 parent 5b43fd7 commit 6203973

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

Design_Patterns_In_Python.tex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,19 @@ \section{Singleton}
180180

181181
This is certainly the most controversial pattern on this list.
182182

183-
This is another easy pattern to implement in Python. In C++ you have to make the constructors
184-
183+
In C++ you have to ensure that a new instance of the singleton cannot be made. This is typically done by protecting the constructors and making a static Instance() function which returns the current instance, or makes a new one if it is appropriate.
184+
185+
This is another easy pattern to implement in Python. It is a case of overriding the \_\_new()\_\_ function. This is a static method for all classes which is called on construction. This function is called with an argument of the class type requested and returns a new instance of that class. So to implement the singleton pattern it is a case of making sure that the \_\_new()\_\_ method returns the global instance. This can be done with the following code:
186+
\begin{lstlisting}
187+
def __new__(new_singleton):
188+
"""Override the __new__ method so that it is a singleton."""
189+
if not new_singleton.instance:
190+
new_singleton.instance = super(Singleton, new_singleton).__new__(new_singleton)
191+
print("An actual new instance of Singleton made.")
192+
print("")
193+
return new_singleton.instance
194+
\end{lstlisting}
195+
Here the class name is Singleton and instance is a member variable initialised to None.
185196

186197
\chapter{Structural}
187198

@@ -231,6 +242,7 @@ \section{Interpreter}
231242
\section{Iterator}
232243
% table of information for the start of a section
233244
\patternSummaryGoalMotProCon{}{}{}{}
245+
This is a pattern which falls into the catagory of included in Python already. This is because anything which can be iterated over is able to be. This goes for built in classes but if you want to make an iterable class you need to supply two methods; \_\_iter\_\_() and next().
234246

235247
\section{Mediator}
236248
% table of information for the start of a section

Iterator.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
11
#!/usr/bin/env python
22
# Written by: DGC
33

4-
class something(object):
5-
6-
def __init__(self):
7-
print("initalised")
4+
class ReverseIterator(object):
5+
"""
6+
Iterates the object given to it in reverse so it shows the difference.
7+
"""
88

9+
def __init__(self, iterable_object):
10+
self.list = iterable_object
11+
self.index = len(iterable_object)
912

13+
def __iter__(self):
14+
return self
15+
16+
def next(self):
17+
" Return the list backwards so it's noticably different."
18+
if (self.index == 0):
19+
# the list is over, raise a stop index exception
20+
raise StopIteration
21+
self.index = self.index - 1
22+
return self.list[self.index]
23+
24+
if (__name__ == "__main__"):
25+
days = [
26+
"Monday",
27+
"Tuesday",
28+
"Wednesday",
29+
"Thursday",
30+
"Friday",
31+
"Saturday",
32+
"Sunday"
33+
]
34+
it = ReverseIterator(days)
35+
for day in it:
36+
print(day)

0 commit comments

Comments
 (0)