1.面向对象编程
Python支持面向对象编程
类(class):现实世界中一些事物的封装 (如:学生)
类:属性 (如:名字,成绩)
类对象
实例对象
引用:通过引用对类的属性和方法进行操作
实例化:创建一个类的具体实例对象 (如:学生张三)
2.装饰器(decorator)
Code:
#Python OO example
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
def introduce(self):
print("hi! I'm " + self.name)
print("my grade is: " + str(self.grade))
def improve(self, amount):
self.grade = self.grade + amount
jim = Student("jim", 86)
jim.introduce()
jim.improve(10)
jim.introduce()
------------------------
def add_candles(cake_func):
def insert_candles():
return cake_func() + " candles"
# 传进来的函数结尾 + candles
return insert_candles
def make_cake():
return "cake"
gift_func = add_candles(make_cake)
print(make_cake()) #cake
print(gift_func()) #cake candles
-------------------------------
def add_candles(cake_func):
def insert_candles():
return cake_func() + " candles"
return insert_candles
def make_cake():
return "cake"
make_cake = add_candles(make_cake)
print(make_cake())
# print(gift_func)
--------------------------
def add_candles(cake_func):
def insert_candles():
return cake_func() + " and candles"
return insert_candles
#加了装饰器,函数就变为add_candles()
#在make_cake()函数主体上加一些小的装饰
@add_candles
def make_cake():
return "cake"
make_cake = add_candles(make_cake)
print(make_cake()) # cake and candles
# print(gift_func)
3.GUI from tkinter
4.逻辑层
Code:
#设置GUI
root = Tk()
w = Label(root, text = "Guess Number Game")
w.pack()
#欢迎消息
mb.showinfo("Welcome", "Welcome to Guess Number Game")
#处理信息
number = 59
while True:
#让用户输入信息
guess = dl.askinteger("Number", "What's your guess?")
if guess == number:
# New block starts here
output = 'Bingo! you guessed it right, but you do not win any prizes!'
mb.showinfo("Hint: ", output)
break
# New block ends here
elif guess < number:
output = 'No, the number is a higer than that'
mb.showinfo("Hint: ", output)
else:
output = 'No, the number is a lower than that'
mb.showinfo("Hint: ", output)
print('Done')
创建简单网页
下载并安装python 2.7 32 bit
下载并安装easy_install windows installer (python 2.7 32bit)
安装 lpthw.web
windows 命令行:
C:\Python27\Scripts\easy_install lpthw.web创建目录
C:\Users\plg519\Maizi\TeachingPython\gotoweb\bin目录下创建 app.py:
import web
urls = (
'/', 'index'
)
app = web.application(urls, globals())
class index:
def GET(self):
greeting = "Hello World"
return greeting
if __name__ == "__main__":
app.run()
6.Windows cmd 运行:
cd C:\Users\plg519\Maizi\TeachingPython\gotoweb\bin
C:\python27\python27 app.py
7.打开浏览器:localhost:8080