py学习笔记:python基础学习

面向对象基础

# __author__ = 'pengwei' 

from abc import ABCMeta, abstractmethod

# 子类1
class Child1:  
    def __init__(self):
        pass

    def print_string(self):
        print("class child1")

# 子类2
class Child2:  
    def __init__(self):
        pass

    def print_string(self):
        print("class child2")

# 多重继承
class Parent(Child2, Child1):  
    # 类属性
    name = 'xxxx'

    def __init__(self):
        Child2.__init__(self)
        # 对象属性
        self.age = 0
        self.__school = ''

    def print_string(self):
        Child2.print_string(self)
        Child1.print_string(self)
        self.age = 1
        self.__school = '123'

    # 静态方法
    @staticmethod
    def static_func():
        Parent.name = 'xx'
        pass

    # 类方法
    @classmethod
    def class_func(cls):
        pass


parent = Parent()  
parent.print_string()  
print(parent.age)  
Parent.static_func()  
print(Parent.name)

# 抽象类
class AbstractCls:  
    __metaclass__ = ABCMeta

    def __init__(self):
        pass

    @abstractmethod
    def get(self): pass


absCls = AbstractCls()  
# absCls.get()
# TypeError: Can't instantiate abstract class AbstractCls with abstract methods get

线程相关

# coding:utf-8
# 给线程加锁
import time  
import thread  
from threading import Lock, Thread


def t(name, x, lock):  
    for i in xrange(x):
        print i, name
        time.sleep(1)  # 暂停1秒
    lock.release()  # 释放锁


lock = thread.allocate_lock()  # 创建锁对象

lock.acquire()  # 加锁

thread.start_new_thread(t, ("动画", 5, lock))  # 运行

while lock.locked():  # 锁状态  
    pass


class MyThread(Thread):  
    """
    利用继承Thread实现多线程
    利用lock解决线程安全
    """

    def run(self):
        lock.acquire()
        print("1234")
        lock.release()
        super(MyThread, self).run()


MyThread().start()


# 线程池
pool = ThreadPool(poolsize)  
requests = makeRequests(some_callable, list_of_args, callback)  
[pool.putRequest(req) for req in requests]  
pool.wait()