python 拾遗

基本语法相关

参考 https://stackoverflow.com/questions/2921847/what-does-the-star-operator-mean-in-python
https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists

函数

参考:https://stackoverflow.com/documentation/python/228/functions

def func(*args):
    # args will be a tuple containing all values that are passed in
    for i in args:
        print(i)
import os
mylist = ["&abcd", "&abbe", "&ab"]
print (os.path.commonprefix(mylist))

参考 https://stackoverflow.com/documentation/python/419/classes

Bound 方法就是通过类的实例访问的方法(instancemethod), unbound 方法就是通过类本身访问的动态方法(Python3.x 已废弃)

a = A()
a.f
# <bound method A.f of <__main__.A object at ...>>
a.f(2)
# 4

# Note: the bound method object a.f is recreated *every time* you call it:
a.f is a.f  # False
# As a performance optimization you can store the bound method in the object's
# __dict__, in which case the method object will remain fixed:
a.f = a.f
a.f is a.f  # True
class Square(Rectangle):
    def __init__(self, s):
        # call parent constructor, w and h are both s
        super(Rectangle, self).__init__(s, s)

        self.s = s
from abc import ABCMeta

class AbstractClass(object):
    # the metaclass attribute must always be set as a class variable
    __metaclass__ = ABCMeta

   # the abstractmethod decorator registers this method as undefined
   @abstractmethod
   def virtual_method_subclasses_must_define(self):
       # Can be left completely blank, or a base implementation can be provided
       # Note that ordinarily a blank interpretation implicitly returns `None`,
       # but by registering, this behaviour is no longer enforced.

class Subclass:
   def virtual_method_subclasses_must_define(self):
       return

# registration is mandatory to truly create an abstract class
AbstractClass.register(SubClass)
class MyClass:

    def __init__(self):
       self._my_string = ""

    @property
    def my_string(self):
        return self._my_string

    @my_string.setter
    def my_string(self, new_value):
        self._my_string = new_value

    @my_string.deleter
    def x(self):
        del self._my_string

装饰器

任何传入一个函数,返回一个函数的函数都可以作为装饰器!(貌似无效!py2.7)

def super_secret_function(f):
    return f

@super_secret_function
def my_function():
    print("This is my secret function.")

这里@ 相当于实现 my_function = super_secret_function(my_function)

装饰器类:__call__ 方法进行装饰。如果需要装饰成员方法,需要指定__get__方法!
装饰器可以使用参数,只要在函数内部定义一个类,然后返回即可。

class Decorator(object):
    """Simple decorator class."""

    def __init__(self, func):
        self.func = func

    def __call__(self, *args, **kwargs):
        print('Before the function call.')
        res = self.func(*args, **kwargs)
        print('After the function call.')
        return res

@Decorator
def testfunc():
    print('Inside the function.')

testfunc()
# Before the function call.
# Inside the function.
# After the function call.

上下文管理器 with

迭代器相关函数

各种坑

In [1]: def append_to_list(value, def_list=[]):
   ...:         def_list.append(value)
   ...:         return def_list
   ...:

In [2]: my_list = append_to_list(1)

In [3]: my_list
Out[3]: [1]

In [4]: my_other_list = append_to_list(2)

In [5]: my_other_list
Out[5]: [1, 2] # 看到了吧,其实我们本来只想生成[2] 但是却把第一次运行的效果页带了进来
In [12]: gen = (i for i in range(5))

In [13]: 2 in gen
Out[13]: True

In [14]: 3 in gen
Out[14]: True

In [15]: 1 in gen
Out[15]: False # 1为什么不在gen里面了? 因为调用1->2,这个时候1已经不在迭代器里面了,被按需生成过了
In [44]: a = [1, 2, 3, 4, 5]

In [45]: for i in a:
   ....:     if not i % 2:
   ....:         a.remove(i)
   ....:

In [46]: a
Out[46]: [1, 3, 5] # 没有问题

In [50]: b = [2, 4, 5, 6]

In [51]: for i in b:
   ....:      if not i % 2:
   ....:          b.remove(i)
   ....:

In [52]: b
Out[52]: [4, 5] # 本来我想要的结果应该是去除偶数的列表
In [55]: my_list = [1, 2, 3, 4, 5]

In [56]: my_list[5] # 根本没有这个元素

In [57]: my_list[5:] # 这个是可以的
In [58]: def my_func():
   ....:         print(var) # 我可以先调用一个未定义的变量
   ....:

In [59]: var = 'global' # 后赋值

In [60]: my_func() # 反正只要调用函数时候变量被定义了就可以了
global

In [61]: def my_func():
   ....:     var = 'locally changed'
   ....:

In [62]: var = 'global'

In [63]: my_func()

In [64]: print(var)

global # 局部变量没有影响到全局变量

In [65]: def my_func():
   ....:         print(var) # 虽然你全局设置这个变量, 但是局部变量有同名的, python以为你忘了定义本地变量了
   ....:         var = 'locally changed'
   ....:

In [66]: var = 'global'

In [67]: my_func()
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-67-d82eda95de40> in <module>()
----> 1 my_func()

<ipython-input-65-0ad11d690936> in my_func()
      1 def my_func():
----> 2         print(var)
      3         var = 'locally changed'
      4

UnboundLocalError: local variable 'var' referenced before assignment

In [68]: def my_func():
   ....:         global var # 这个时候得加全局了
   ....:         print(var) # 这样就能正常使用
   ....:         var = 'locally changed'
   ....:

In [69]: var = 'global'

In [70]:

In [70]: my_func()
global

In [71]: print(var)
locally changed # 但是使用了global就改变了全局变量

工具函数

from inspect import getmembers
for k,v in (getmembers(object)):
    print k,':', v

logging

python打日志模块

import logging
logging.