-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.py
More file actions
181 lines (128 loc) · 2.88 KB
/
Copy pathclass.py
File metadata and controls
181 lines (128 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def scope_test():
def do_local():
spam = "local spam"
def do_nonlocal():
nonlocal spam
spam = "nonlocal spam"
def do_global():
global spam
spam = "global spam"
spam = "test spam"
do_local()
print("After local assignment: ", spam)
do_nonlocal()
print("After non local assignment: ", spam )
do_global()
print("After global assignment: ", spam)
scope_test()
print("In global scope: ", spam)
class MyClass:
""" A simple example class """
i = 12345
def f(self):
return "Hello World"
x = MyClass()
class Complex:
def __init__(self, realpart, imagpart):
self.r = realpart
self.i = imagpart
x = Complex(3.0, -4.5)
x.r, x.i
x.counter = 1
while x.counter < 10:
x.counter *= 2
print(x.counter)
del x.counter
class Warehouse:
purpose = "storage"
region = "West"
w1 = Warehouse()
print(w1.purpose, w1.region)
w2 = Warehouse()
w2.region = "east"
print(w2.purpose, w2.region)
"""
class Dog:
def __init(self, name):
self.name = name
self.tricks = [ ]
def add_trick(self, trick):
self.tricks.append(trick)
d = Dog('Fido')
e = Dog("Buddy")
d.add_trick("roll over.")
e.add_trick("play dead")
d.tricks
e.tricks
"""
def f1(self, x, y):
return min(x, x+y)
class C:
f = f1
def g(self):
return "Hello World"
h = g
class Bag:
def __init__ (self):
self.data = [ ]
def add(self, x):
self.data.append(x)
def addtwice(self, x):
self.add(x)
self.add(x)
class Mapping:
def __init__(self, iterable):
self.items_list = [ ]
self.__update(iterable)
def update(self, iterable):
for item in iterable:
self.items_list.append(item)
__update = update
class MappingSubClass(Mapping):
def update(self, keys, values):
for item in zip(keys, values):
self.items_list.append(item)
class Employee:
pass
John = Employee()
John.name = "John Doe"
John.dept = "Finance"
John.salary = 500
print(John.salary)
for element in [1, 2, 3]:
print(element)
for key in {"one": 1, "two": 2}:
print(key)
for char in "123":
print(char)
# for line in open("oop.txt"):
# print(line, end=' ')
class Reverse:
""" Iterator for looping over a sequence backwards. """
def __init__(self, data):
self.data = data
self.index = len(data)
def __iter__(self):
return self
def __next__(self):
if self.index == 0:
raise stopIteration
self.index = self.index - 1
return self.data[self.index]
rev = Reverse("spam")
iter(rev)
#for char in rev:
# print(char)
def reverse(data):
for index in range(len(data) - 1, -1, -1):
yield data[index]
for char in reverse("golf"):
print(char)
sum(i*i for i in range(10))
xvec = [10, 20, 30]
yvec = [7, 5, 3]
print(sum(x*y for x, y in zip(xvec, yvec)))
# unique_words = set(word for line in page for word in line .split())
# valedictorian = max((student.gpa, student.name) for student in graduates)
data = "golf"
print(list(data[i] for i in range(len(data) -1, -1, -1)))