Skip to content

Commit 8416ac0

Browse files
authored
1 parent d052e24 commit 8416ac0

5 files changed

+247
-0
lines changed

Composition_Obj_Constrcution.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Node(object):
2+
def __init__(self,sName):
3+
self._lChildren = []
4+
self.sName = sName
5+
def __repr__(self):
6+
return "<Node '{}'>".format(self.sName)
7+
def append(self,*args,**kwargs):
8+
self._lChildren.append(*args,**kwargs)
9+
def print_all_1(self):
10+
print(self)
11+
for oChild in self._lChildren:
12+
oChild.print_all_1()
13+
def print_all_2(self):
14+
def gen(o):
15+
lAll = [o,]
16+
while lAll:
17+
oNext = lAll.pop(0)
18+
lAll.extend(oNext._lChildren)
19+
print lAll
20+
yield oNext
21+
for oNode in gen(self):
22+
print(oNode)
23+
24+
oRoot = Node("root")
25+
oChild1 = Node("child1")
26+
oChild2 = Node("child2")
27+
oChild3 = Node("child3")
28+
oChild4 = Node("child4")
29+
oChild5 = Node("child5")
30+
oChild6 = Node("child6")
31+
oChild7 = Node("child7")
32+
oChild8 = Node("child8")
33+
oChild9 = Node("child9")
34+
oChild10 = Node("child10")
35+
36+
oRoot.append(oChild1)
37+
oRoot.append(oChild2)
38+
oRoot.append(oChild3)
39+
oChild1.append(oChild4)
40+
oChild1.append(oChild5)
41+
oChild2.append(oChild6)
42+
oChild4.append(oChild7)
43+
oChild3.append(oChild8)
44+
oChild3.append(oChild9)
45+
oChild6.append(oChild10)
46+
47+
# specify output from here onwards
48+
49+
oRoot.print_all_1()
50+
oRoot.print_all_2()

Inheritence_Super.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class A(object):
2+
def go(self):
3+
print("go A go!")
4+
def stop(self):
5+
print("stop A stop!")
6+
def pause(self):
7+
raise Exception("Not Implemented")
8+
9+
class B(A):
10+
def go(self):
11+
super(B, self).go()
12+
print("go B go!")
13+
14+
class C(A):
15+
def go(self):
16+
super(C, self).go()
17+
print("go C go!")
18+
def stop(self):
19+
super(C, self).stop()
20+
print("stop C stop!")
21+
22+
class D(B,C):
23+
def go(self):
24+
super(D, self).go()
25+
print("go D go!")
26+
def stop(self):
27+
super(D, self).stop()
28+
print("stop D stop!")
29+
def pause(self):
30+
print("wait D wait!")
31+
32+
class E(B,C): pass
33+
34+
a = A()
35+
b = B()
36+
c = C()
37+
d = D()
38+
e = E()
39+
40+
# specify output from here onwards
41+
42+
a.go() #A
43+
b.go() #A B
44+
c.go() #A C
45+
d.go() #A C B D
46+
e.go() #A C B
47+
48+
a.stop() #A
49+
b.stop() #A
50+
c.stop() #A C
51+
d.stop() #A C D
52+
e.stop() #A C
53+
54+
a.pause()
55+
b.pause()
56+
c.pause()
57+
d.pause()
58+
e.pause()

ex2.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from collection import OrderedDict
2+
f = file('ex2.txt','r');
3+
dict1 = {}
4+
5+
for line in f.readlines():
6+
tokens = line.split(':',3)
7+
dict1[tokens[0]] = int(tokens[2])
8+
9+
print OrderedDict(sorted(dict1.items(),key=lambda x: t[0]))
10+

static_class_method.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
class MyClass(object):
2+
def __init__(self):
3+
self._some_property = "properties are nice"
4+
self._some_other_property = "VERY nice"
5+
def normal_method(*args,**kwargs):
6+
print("calling normal_method({0},{1})".format(args,kwargs))
7+
@classmethod
8+
def class_method(*args,**kwargs):
9+
print("calling class_method({0},{1})".format(args,kwargs))
10+
@staticmethod
11+
def static_method(*args,**kwargs):
12+
print("calling static_method({0},{1})".format(args,kwargs))
13+
@property
14+
def some_property(self,*args,**kwargs):
15+
print("calling some_property getter({0},{1},{2})".format(self,args,kwargs))
16+
return self._some_property
17+
@some_property.setter
18+
def some_property(self,*args,**kwargs):
19+
print("calling some_property setter({0},{1},{2})".format(self,args,kwargs))
20+
self._some_property = args[0]
21+
@property
22+
def some_other_property(self,*args,**kwargs):
23+
print("calling some_other_property getter({0},{1},{2})".format(self,args,kwargs))
24+
return self._some_other_property
25+
26+
o = MyClass()
27+
# undecorated methods work like normal, they get the current instance (self) as the first argument
28+
29+
o.normal_method
30+
# <bound method MyClass.normal_method of <__main__.MyClass instance at 0x7fdd2537ea28>>
31+
32+
o.normal_method()
33+
# normal_method((<__main__.MyClass instance at 0x7fdd2537ea28>,),{})
34+
35+
o.normal_method(1,2,x=3,y=4)
36+
# normal_method((<__main__.MyClass instance at 0x7fdd2537ea28>, 1, 2),{'y': 4, 'x': 3})
37+
38+
# class methods always get the class as the first argument
39+
40+
o.class_method
41+
# <bound method classobj.class_method of <class __main__.MyClass at 0x7fdd2536a390>>
42+
43+
o.class_method()
44+
# class_method((<class __main__.MyClass at 0x7fdd2536a390>,),{})
45+
46+
o.class_method(1,2,x=3,y=4)
47+
# class_method((<class __main__.MyClass at 0x7fdd2536a390>, 1, 2),{'y': 4, 'x': 3})
48+
49+
# static methods have no arguments except the ones you pass in when you call them
50+
51+
o.static_method
52+
# <function static_method at 0x7fdd25375848>
53+
54+
o.static_method()
55+
# static_method((),{})
56+
57+
o.static_method(1,2,x=3,y=4)
58+
# static_method((1, 2),{'y': 4, 'x': 3})
59+
60+
# properties are a way of implementing getters and setters. It's an error to explicitly call them
61+
# "read only" attributes can be specified by creating a getter without a setter (as in some_other_property)
62+
63+
o.some_property
64+
# calling some_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{})
65+
# 'properties are nice'
66+
67+
o.some_property()
68+
# calling some_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{})
69+
# Traceback (most recent call last):
70+
# File "<stdin>", line 1, in <module>
71+
# TypeError: 'str' object is not callable
72+
73+
o.some_other_property
74+
# calling some_other_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{})
75+
# 'VERY nice'
76+
77+
# o.some_other_property()
78+
# calling some_other_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{})
79+
# Traceback (most recent call last):
80+
# File "<stdin>", line 1, in <module>
81+
# TypeError: 'str' object is not callable
82+
83+
o.some_property = "groovy"
84+
# calling some_property setter(<__main__.MyClass object at 0x7fb2b7077890>,('groovy',),{})
85+
86+
o.some_property
87+
# calling some_property getter(<__main__.MyClass object at 0x7fb2b7077890>,(),{})
88+
# 'groovy'
89+
90+
o.some_other_property = "very groovy"
91+
# Traceback (most recent call last):
92+
# File "<stdin>", line 1, in <module>
93+
# AttributeError: can't set attribute
94+
95+
o.some_other_property
96+
# calling some_other_property getter(<__main__.MyClass object at 0x7fb2b7077890>,(),{})
97+
# 'VERY nice'

variable_arguments.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def f(*args,**kwargs): print(args, kwargs)
2+
3+
l = [1,2,3]
4+
t = (4,5,6)
5+
d = {'a':7,'b':8,'c':9}
6+
7+
f()
8+
f(1,2,3) # (1, 2, 3) {}
9+
f(1,2,3,"groovy") # (1, 2, 3, 'groovy') {}
10+
f(a=1,b=2,c=3) # () {'a': 1, 'c': 3, 'b': 2}
11+
f(a=1,b=2,c=3,zzz="hi") # () {'a': 1, 'c': 3, 'b': 2, 'zzz': 'hi'}
12+
f(1,2,3,a=1,b=2,c=3) # (1, 2, 3) {'a': 1, 'c': 3, 'b': 2}
13+
14+
f(*l,**d) # (1, 2, 3) {'a': 7, 'c': 9, 'b': 8}
15+
f(*t,**d) # (4, 5, 6) {'a': 7, 'c': 9, 'b': 8}
16+
f(1,2,*t) # (1, 2, 4, 5, 6) {}
17+
f(q="winning",**d) # () {'a': 7, 'q': 'winning', 'c': 9, 'b': 8}
18+
f(1,2,*t,q="winning",**d) # (1, 2, 4, 5, 6) {'a': 7, 'q': 'winning', 'c': 9, 'b': 8}
19+
20+
def f2(arg1,arg2,*args,**kwargs): print(arg1,arg2, args, kwargs)
21+
22+
f2(1,2,3) # 1 2 (3,) {}
23+
f2(1,2,3,"groovy") # 1 2 (3, 'groovy') {}
24+
f2(arg1=1,arg2=2,c=3) # 1 2 () {'c': 3}
25+
f2(arg1=1,arg2=2,c=3,zzz="hi") # 1 2 () {'c': 3, 'zzz': 'hi'}
26+
f2(1,2,3,a=1,b=2,c=3) # 1 2 (3,) {'a': 1, 'c': 3, 'b': 2}
27+
28+
f2(*l,**d) # 1 2 (3,) {'a': 7, 'c': 9, 'b': 8}
29+
f2(*t,**d) # 4 5 (6,) {'a': 7, 'c': 9, 'b': 8}
30+
f2(1,2,*t) # 1 2 (4, 5, 6) {}
31+
f2(1,1,q="winning",**d) # 1 1 () {'a': 7, 'q': 'winning', 'c': 9, 'b': 8}
32+
f2(1,2,*t,q="winning",**d) # 1 2 (4, 5, 6) {'a': 7, 'q': 'winning', 'c': 9, 'b': 8}

0 commit comments

Comments
 (0)