class MyRange(object):
def __init__(self, n):
self.idx = 0
self.n = n
def __iter__(self):
return self
def __next__(self):
if self.idx < self.n:
val = self.idx
self.idx += 1
return val
else:
raise StopIteration()
myRange = MyRange(3)
print (next(myRange))
print (next(myRange))
print (next(myRange))
class MyRange(object):
def __init__(self, n):
self.idx = 0
self.n = n
def __iter__(self):
return self
def __next__(self):
try:
if self.idx < self.n:
val = self.idx
self.idx += 1
return val
except:
raise StopIteration()
myRange = MyRange(3)
print (next(myRange))
print (next(myRange))
print (next(myRange))
print (next(myRange))
print("************")
def odd():
print ('step 1')
yield 1
print ('step 2')
yield 3
print ('step 3')
yield 5
o = odd()
print (next(o))
print (next(o))
print (next(o))
for i in o:
print (i) #可以遍历执行函数
>>> a=[1,2,3,[4,5]]
>>> b=a
>>> import copy
>>> c=copy.copy(a)
>>> d=copy.deepcopy(a)
>>> a.append("a")
>>> a
[1, 2, 3, [4, 5], 'a']
>>> b
[1, 2, 3, [4, 5], 'a']
>>> c
[1, 2, 3, [4, 5]]
>>> d
[1, 2, 3, [4, 5]]
>>> a[3].append("b")
>>> a
[1, 2, 3, [4, 5, 'b'], 'a']
>>> b
[1, 2, 3, [4, 5, 'b'], 'a']
>>> c
[1, 2, 3, [4, 5, 'b']]
>>> d
[1, 2, 3, [4, 5]]
>>> a=["a",[1,2]]
>>> b=copy.copy(a)
>>> c=copy.deepcopy(a)
>>> id(a)
2689090637064
>>> id(b)
2689090637128
>>> id(c)
2689089896072
>>> id(a[0])
2689089727536
>>> id(b[0])
2689089727536
>>> id(c[0])
2689089727536
>>> id(a[1])
2689090634056
>>> id(b[1])
2689090634056
>>> id(c[1])
2689090637320
>>> a[0]=10
>>> a
[10, [1, 2]]
>>> b
['a', [1, 2]]
>>> c
['a', [1, 2]]
>>> a.append("c")
>>> a
[10, [1, 2], 'c']
>>> b
['a', [1, 2]]
>>> c
['a', [1, 2]]
>>> a
[10, [1, 2], 'c']
>>> d=a[:]
>>> a.append("b")
>>> a
[10, [1, 2], 'c', 'b']
>>> d
[10, [1, 2], 'c']
>>> a[1].append(3)
>>> a
[10, [1, 2, 3], 'c', 'b']
>>> d
[10, [1, 2, 3], 'c']
queue=[]
while 1:
value=input('请输入:')
if value=='done':
break
queue.insert(0,value)
while 1:
if len(queue)>0:
value=queue.pop()
print('get value:%s' %value)
else:
break
queue=[]
while 1:
a=input('请输入:')
if a=='end':
break
queue.append(a)
while 1:
if len(queue)>0:
print(queue.pop())
else:
break