Each pattern has:
The two important attributes of Singleton are:
Two important attributes of Command:
``Encapsulation by courtesy'' refers to the fact that, prior to Python release 1.4, there was no facility for information hiding. Thus, it was up to the programmer to use get and set functions to access data in a class. Python releases after 1.4 provide name mangling to enforce encapsulation.
The idea is to create a system that can serve different requests in a hierarchical manner. That is, if an object does not know how to handle a request, it passes it along the object tree until an object is found that can handle the request.
1 import os
2 import string
3 print 15/4
4 str = "This is a sentence."
5 print str[-5]
6 L = string.split(str, ' ') # it's a blank!
7 print L
8 print len(L)
**************************** OUTPUT ****************************
3
e
['This', 'is', 'a', 'sentence.']
4
****************************************************************
class First:
__name = "Microsoft"
def __init__(self, n, x):
self.__name = n
self.__number = x
def setData(self, val):
self.__number = val
def __repr__(self):
return self.__name+"\t"+str(self.__number)
x = First("Dixie Chicks", 100)
print x
****************************************************************
Dixie Chicks 100
**************************** OUTPUT ****************************
import string
class Music:
def __init__(self):
self.wds = []
self.L = ["I'm not ready to make nice",
"I'm not ready to back down",
"I'm still mad as hell."
]
def stuff(self, num=15):
for count in range(0, len(self.L)):
self.wds += string.split(self.L[count], ' ')
while self.wds:
print self.wds[0:num]
self.wds = self.wds[num:]
if __name__ == '__main__':
M = Music()
M.stuff(3)
****************************************************************
["I'm", 'not', 'ready']
['to', 'make', 'nice']
["I'm", 'not', 'ready']
['to', 'back', 'down']
["I'm", 'still', 'mad']
['as', 'hell.']
**************************** OUTPUT ****************************
word = raw_input("Type a word: ")
print word
**************************** ANSWER ****************************
1 for x in range(0, len(word)):
2 print word[-1]
3 word = word[0:len(word)-1]
****************************************************************
**************************** ANSWER ****************************
from Tkinter import *
import os
class GUI:
def __init__(self, root):
label = Label(root, text="The Dixie Chicks Rock")
label.pack(side=TOP)
but1 = Button(root, text="quit", command=sys.exit)
but1.pack(side=TOP)
but2 = Button(root, text="print", command=self.display)
but2.pack(side=TOP)
def display(self):
print "Hello World"
if __name__ == "__main__":
root = Tk()
gui = GUI(root)
root.mainloop()
****************************************************************
**************************** ANSWER ****************************
class First:
__name = "Microsoft"
__single = None
def __init__(self, n, x):
if First.__single:
raise First.__single
self.__name = n
self.__number = x
First.__single = self
def setData(self, val):
self.__number = val
def __repr__(self):
return self.__name+"\t"+str(self.__number)
def Handle( a, b, x = First ):
try: single = x(a, b)
except First, s:
single = s
return single
x = Handle( "Dixie Chicks", 100 )
print x
y = Handle( "Rufus Wainwright", 99 )
print y
****************************************************************