python学习总结---学习交流群里的问题总结

连续添加数据到明细:

def onchange_product_id(self, cr, uid,ids, product_id,line_id, context=None):
        result={}
        if product_id:
            sql="select product_id, sum (qty) qty,lot_id ,max(in_date) in_date ,location_id from stock_quant where product_id=%d GROUP by product_id,lot_id ,location_id;"%(product_id)
            cr.execute(sql)
            dict=cr.dictfetchall()
# 这一句将原开数据,一直叠加进去 line_ids
=line_id num=len(dict) i=0 for i in range(num): line_ids.append(({ 'sequence':i+1, 'product_id':dict[i]['product_id'], 'lot_id':dict[i]['lot_id'], 'wl_qty':dict[i]['qty'], 'realy_qty':dict[i]['qty'], 'date_in':dict[i]['in_date'], 'location_id':dict[i]['location_id'], })) i+=1 result['line_id']=line_ids return {'value':result}

做odoo一年多了,今天碰到学习交流群里的一些问题:这里将它记录下来

当函数的参数不确定时,可以使用*args 和**kwargs,*args 没有key值,**kwargs有key值。

def fun_var_args(farg, *args): 
    print "arg:", farg 
    for value in args: 
        print "another arg:", value 
 
fun_var_args(1, "two", 3) # *args可以当作可容纳多个变量组成的list 
result:
[python] 
arg: 1 
another arg: two 
another arg: 3 
**kwargs:
[python] 
def fun_var_kwargs(farg, **kwargs): 
    print "arg:", farg 
    for key in kwargs: 
        print "another keyword arg: %s: %s" % (key, kwargs[key]) 
 
 
fun_var_kwargs(farg=1, myarg2="two", myarg3=3) # myarg2和myarg3被视为key, 感觉**kwargs可以当作容纳多个key和value的dictionary 
result:
[python] 
arg: 1 
another keyword arg: myarg2: two 
another keyword arg: myarg3: 3 
也可以用下面的形式:
[python] 
def fun_var_args_call(arg1, arg2, arg3): 
    print "arg1:", arg1 
    print "arg2:", arg2 
    print "arg3:", arg3 
 
args = ["two", 3] #list
fun_var_args_call(1, *args) 
result:
[python] 
arg1: 1 
arg2: two 
arg3: 3 

#这里的**kwargs可以是字典类型参数

#简单的例子

[python] 
def fun_var_args_call(arg1, arg2, arg3): 
    print "arg1:", arg1 
    print "arg2:", arg2 
    print "arg3:", arg3 
 #命名变量
kwargs = {"arg3": 3, "arg2": "two"} # dictionary 
 

#调用函数
fun_var_args_call(1, **kwargs) 
result:
[python] view plaincopyprint?
arg1: 1 
arg2:"two" 
arg3:3







python中的单例模式:

单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在。当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场。

实现单例模式的几种形式:

1.使用模块

其实,Python 的模块就是天然的单例模式

模块在第一次导入时,会生成 .pyc 文件,当第二次导入时,就会直接加载 .pyc 文件,

而不会再次执行模块代码。因此,我们只需把相关的函数和数据定义在一个模块中,就可以获得一个单例对象了

mysingleton.py

class Singleton(object):
    def foo(self):
        pass singleton = Singleton()

将上面的代码保存在文件 mysingleton.py 中,要使用时,直接在其他文件中导入此文件中的对象,这个对象即是单例模式的对象

调用时用:   from a import singleton



2.使用装饰器

 
复制代码
 
def Singleton(cls):
    _instance = {}

    def _singleton(*args, **kargs):
        if cls not in _instance: _instance[cls] = cls(*args, **kargs) return _instance[cls] return _singleton #运行时,会现加载他 @Singleton class A(object): a = 1 def __init__(self, x=0): self.x = x a1 = A(2) a2 = A(3)




 
 

3.使用类

 
 
复制代码
 
 
class Singleton(object):

    def __init__(self):
        pass @classmethod #类方法 def instance(cls, *args, **kwargs): if not hasattr(Singleton, "_instance"): Singleton._instance = Singleton(*args, **kwargs) return Singleton._instance






























猜你喜欢

转载自www.cnblogs.com/1314520xh/p/9026987.html