python语法记录
class threading.Condition(lock=None)
This class implements condition variable objects. A condition variable allows one or more threads to wait until they are notified by another thread.
If the lock argument is given and not None, it must be a Lock or RLock object, and it is used as the underlying lock.
Otherwise, a new RLock object is created and used as the underlying lock.
也就是说,如果condition构造函数lock参数为空的话,会自动创建可重入锁RLock。
可重入锁RLock,同一线程可以多次获取(the same thread may acquire it again without blocking)。
在stackOverflow上看到的,python 2.5加了的if else实现类似三目运算符的功能:
a if condition else b
First condition is evaluated, then either a or b is returned based on the Boolean value of condition
If condition evaluates to True a is returned, else b is returned.
甚至可以这样 : 1 if a > b else -1 if a < b else 0
getattr、hasattr(它就是调用getattr,不抛出异常就返回True)setattr、delattr。
partial:
functools.partial(func[,*args][, **keywords])
Return a new partial object which when called will behave like func called with the positional arguments args and keyword arguments keywords. If more arguments are supplied to the call, they are appended to args. If additional keyword arguments are supplied, they extend and override keywords. Roughly equivalent to:
def partial(func, *args, **keywords):
def newfunc(*fargs, **fkeywords):
newkeywords = keywords.copy()
newkeywords.update(fkeywords)
return func(*(args + fargs), **newkeywords)
newfunc.func = func
newfunc.args = args
newfunc.keywords = keywords
return newfunc
inspect.signature,可以从一个可调用对象提取参数签名信息。bind_partial()和bind()方法对提供的类型到参数名绑定,生成字典。
python lamda:
g = lambda x:x+1
g(1)
>>>2
g(2)
>>>3
lambda x:x+1(1)
>>>2
__getattribute__方法,我尝试在其中访问self.name,出现了异常RuntimeError: maximum recursion depth exceeded while calling a Python object。递归深度超出。
def log_getattribute(cls):
orig_getattribute = cls.__getattribute__
def new_getattribute(self, name):
print 'getting:',name,self.name
return orig_getattribute(self, name)
cls.__getattribute__= new_getattribute
return cls
难道这就是传说中的闭包???