zoukankan      html  css  js  c++  java
  • 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

    难道这就是传说中的闭包???

  • 相关阅读:
    kafka学习总结010 --- 实际项目中遇到的问题1
    kafka学习总结009 --- HW和LEO
    spring学习总结001 --- IOC控制反转、DI依赖注入
    kafka学习总结008 --- 生产者生产数据流程(参照源码)
    kafka学习总结007 --- 生产者Java API实例
    kafka学习总结006 --- 生产者事务
    kafka学习总结005 --- at-exactly-once语义
    kafka学习总结004 --- 生产者ISR
    kafka学习总结003 --- 生产者分区策略
    计算机基础-1(进制转换)
  • 原文地址:https://www.cnblogs.com/ph829/p/7113282.html
Copyright © 2011-2022 走看看