反射 hasattr,getattr
class Foo:
static_name = 'nba'
def __init__(self):
self.name = 'alex'
def show(self):
pass
@staticmethod
def static_show():
pass
@classmethod
def class_show(cls):
pass
obj = Foo()
print hasattr(Foo,'name') --> False #当判断Foo类中是否有对象的属性时,只在类中找,不去对象中找
print hasattr(Foo,'static_name') --> True
print hasattr(obj,'name') --> True
print hasattr(obj,'static_name') --> True #当判断obj对象中是否有static_name属性时,先在对象中找,然后去类中找
'''三引号:
1.注释
2.可换行字符串
msg = '''
start :start ftp server
stop :stop ftp server
create_account :create ftp user account
help :print help msg
'''
简单线程池版本一:
class ThreadPool(object):
def __init__(self,max_num=20):
self.queue = Queue.Queue(max_num)
for i in xrange(max_num):
self.queue.put(threading.Thread)
def get_thread(self):
return self.queue.get()
def add_thread(self):
return self.queue.put(threading.Thread)
def count_thread(self):
return self.queue.qsize()
pool = ThreadPool(10)
def func(arg,p):
print arg
import time
time.sleep(2)
p.add_thread() #Queue添加了一个线程,10个
for i in xrange(30):
thread = pool.get_thread() #Queue拿走了一个线程,9个
t = thread(target= func, args= (i,pool))
t.start()