高阶函数
函数实质是一个变量,fun -> 内存空间; func1 = fun; func1()
高阶函数:可以接受函数作为参数,或者把函数作为返回值的函数;
def fun1(var):
return var
def fun1(fun2):
…..
return fun2
In [1]: abs(-1)
Out[1]: 1
In [2]: a(-1)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-c976f5e459ea> in <module>()
----> 1 a(-1)
NameError: name 'a' is not defined
In [3]: a = abs
In [4]: a(-1)
Out[4]: 1
面向对象编程
- 面向过程编程:
- 面向对象编程(OOP) Object Oriented Programming
- 面向函数编程:(lisp)
(13+10)*2-10
a = 13+10
b = a*2
c = b-10
result = jian(multi(add(13,10),2),10)
作业:通过面向对象的方式实现队列和栈的数据结构
class Stact(object):
le=[1,24,5,”asd”]
def
init
(self):
pass
def push(self):
item=raw_input(“请输入你想加如的内容”)
self.le.append(item)
def pop(self):
if self.le==[]:
print “none”
else:
self.le.pop(-1)
return self.le
def stack_top(self):
if not self.le==[]:
print “none”
else:
print self.le[-1]
def length(self):
d=len(self.le)
print d
def info(self):
for i in self.le:
print i
s=Stact()
s.info()
print s.pop()
s.info()
游戏:
pr=”“”
welcome ying xiong lian meng
qin
hu
an
please your role:”“”
changjing=”“”
mojiajiguan
chang ping
wangzhe
shenyuan
huoyanshan
please you choice:”“”
class People(object):
def
init
(self,name,age,sex,value):
self.name=name
self.age=age
self.sex=sex
self.value=value
def moji(self):
print """
huan ying lai dao mojia jiguan
"""
self.value-=100
return "\t%s,%s,%s,%s"%(self.name,self.age,self.sex,self.value)
def changping(self):
print """
huan ying lai dao changping
"""
self.value-=200
return "\t%s,%s,%s,%s"%(self.name,self.age,self.sex,self.value)
def wangzhe(self):
print """
huan ying lai dao wangzhe
"""
self.value-=150
return "\t%s,%s,%s,%s"%(self.name,self.age,self.sex,self.value)
def shenyuan(self):
print """
huan ying lai dao shenyuan
"""
self.value-=300
return "\t%s,%s,%s,%s"%(self.name,self.age,self.sex,self.value)
def huoyan(self):
print """
huan ying lai dao huoyan
"""
self.value-=500
return "\t%s,%s,%s,%s"%(self.name,self.age,self.sex,self.value)
# dic={"M":moji,
# "C":changping,
# "W":wangzhe,
# "S":shenyuan,
# "H":huoyan}
qin = People(“qin”,18,”nan”,1000)
hu=People(“hu”,18,”nv”,2000)
an=People(“an”,18,”an”,2500)
while 1:
choice=raw_input(pr)
if choice==”qin”:
changjing=raw_input(changjing)
if changjing==”mojiajiguan”:
d=qin.moji()
print d
elif changjing==”changping”:
d=qin.changping()
print d
elif changjing==”wangzhe”:
d=qin.wangzhe()
print d
elif changjing==”shenyuan”:
d=qin.shenyuan()
print d
elif changjing==”huoyanshan”:
d=qin.huoyan()
print d
elif choice==”hu”:
changjing=raw_input(changjing)
if changjing==”mojiajiguan”:
d=hu.moji()
print d
elif changjing==”changping”:
d=hu.changping()
print d
elif changjing==”wangzhe”:
d=hu.wangzhe()
print d
elif changjing==”shenyuan”:
d=hu.shenyuan()
print d
elif changjing==”huoyanshan”:
d=hu.huoyan()
print d
elif choice==”an”:
changjing=raw_input(changjing)
if changjing==”mojiajiguan”:
d=an.moji()
print d
elif changjing==”changping”:
d=an.changping()
print d
elif changjing==”wangzhe”:
d=an.wangzhe()
print d
elif changjing==”shenyuan”:
d=an.shenyuan()
print d
elif changjing==”huoyanshan”:
d=an.huoyan()
print d