Summary of questions in Python learning summary---learning Communication Group

Source: Internet
Author: User

Continuously add data to detail:

defONCHANGE_PRODUCT_ID (Self, CR, Uid,ids, product_id,line_id, context=None): Result={}        ifProduct_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 ()
# This sentence will be the original data, has been superimposed in line_ids = line_id num=Len (dict) I=0 forIinchrange (num): Line_ids.append (({'sequence': i+1, 'product_id':d ict[i]['product_id'], 'lot_id':d ict[i]['lot_id'], 'Wl_qty':d ict[i]['Qty'], 'Realy_qty':d ict[i]['Qty'], 'date_in':d ict[i]['in_date'], 'location_id':d ict[i]['location_id'],})) I+=1result['line_id']=Line_idsreturn{'value': result}

Odoo for more than a year, today encountered some problems in the Learning Exchange group: here to record it down

when the parameters of a function are indeterminate, you can use *args and **kwargs,*args without a key value, **kwargs has a key value. 

def Fun_var_args (Farg, *args):
Print "arg:", Farg
For value in args:
Print "Another arg:", value

Fun_var_args (1, "I", 3) #*args can be used as a list that can accommodate multiple variables
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= "a", myarg3=3)# Myarg2 and Myarg3 are considered key, feeling that **kwargs can be used to accommodate multiple keys and value dictionary
Result
[Python]
Arg:1
Another keyword Arg:myarg2:two
Another keyword Arg:myarg3:3
You can also use the following form:
[Python]
def fun_var_args_call (Arg1, Arg2, ARG3):
Print "Arg1:", arg1
Print "Arg2:", arg2
Print "Arg3:", Arg3

args = ["Both", 3] #list
Fun_var_args_call (1, *args)
Result
[Python]
Arg1:1
Arg2:two
Arg3:3

#这里的 **kwargs can be a dictionary type parameter

#简单的例子

[Python]
def fun_var_args_call (Arg1, Arg2, ARG3):
Print "Arg1:", arg1
Print "Arg2:", arg2
Print "Arg3:", Arg3
#命名变量
Kwargs = {"Arg3": 3, "arg2": "Both"} # Dictionary

#调用函数
Fun_var_args_call (1, **kwargs)
Result
[Python] View plaincopyprint?
Arg1:1
ARG2: "The Other"
Arg3:3







A singleton pattern in Python:

The singleton pattern (Singleton pattern) is a commonly used software design pattern that is primarily intended to ensure that only one instance of a class exists . A singleton can come in handy when you want the entire system to have only one instance of a class.

Several forms of implementing a singleton pattern:

1. Using the module

In fact,Python's modules are natural singleton patterns.

When the module is first imported, the file is generated, and .pyc when the second import, the file is loaded directly .pyc ,

The module code is not executed again. So, we just need to define the relevant functions and data in a module, we can get a singleton object.

mysingleton.py

Class Singleton (object):    def foo (self):        passSingleton = Singleton ()  

Save the above code in a file mysingleton.py , and when you want to use it, import the object from the file directly in the other file, which is the object of the singleton schema

called with:     Import   Singleton



2. Using adorners
Def Singleton (CLS): _instance = {} def _singleton (*args, **kargs): if cls not in _instance: _instance[cls] = CLS (*args , **kargs) return _ INSTANCE[CLS] return _singleton# runtime, it will now load him @singleton< Span style= "COLOR: #0000ff" >class A (object): A = 1 def __init__ (self, X=0): self.x = XA1 = A (2          




3. Using the class
 class Singleton (object): def __init__pass @classmethod #类方法 def Instance (CLS, *args, **kwargs): if not hasattr (Singleton, " _instance" ): Singleton._instance = Singleton (*args, * * kwargs) return singleton._instance          






























Summary of questions in Python learning summary---learning Communication Group

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.