python的函数以及多线程
程序员文章站
2024-03-23 18:16:22
...
- 1.支持lambda表达式,很类似于scala
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# @Time : 17/11/15 上午11:16
# @Author : lijie
# @File : mytest02.py
##函数
def func01():
print "hello world"
def func02(str):
print str
def func03(x,y):
print x+y
def func04(myfunc01,x,y):
print myfunc01(x,y)
def func05(name,age=25):
print name,age
def func06(name, *myvar):
for a in myvar:
print name,a
func01()
func02("hehe")
func03(1,2)
func04(lambda x,y:x+y,2,5)
func05("lijie")
func05("lijie",100)
func06("lijie","a","b",1,2,3)
- 2.python的线程demo,但是Python的多线程是程序自己模拟的,非利用cpu的时间片轮转
##多线程
import threading
import time
def thread01(args):
for i in range(1,10):
print "thread 01 ",args," ",i
time.sleep(1)
def thread02(args):
for i in range(1,10):
print "thread 02 ",args," ",i
time.sleep(1)
threads=[]
##注意一定args=(u'one',) 一个参数时候一定要加上,
##传入的参数要求是tuple,在python用(1,)表示一个tuple
t1=threading.Thread(target=thread01,args=(u'one',))
t2=threading.Thread(target=thread02,args=(u'two',))
threads.append(t1)
threads.append(t2)
for i in threads:
# i.setDaemon(True)
i.start()
print "main"
下一篇: Python的多线程