python - return data from functions that accept optional named paramaters, **kwargs -


I'm just learning about ** kwargs and I'm probably running over this real use, so here goes

I have 1 main script, 3 functions and one trunk: script.py, setup (), billing (), new cost (), data = {}

< P> Setup call billing () and billing () will cost (new), and the new cast will return an ID that will be used as setup.

Each function will accept the optional name parameter, ** the quad, which this dictates, data = {}

Ink passes through the bus, Does not hit the new cost (), where I manipulate data and wish to return some other data. But it seems that the new cost () is unable to return its data, when a variable is specified the data is passing properly.

  def setup (** kwargs): ID = billing (** kwargs) analytics (id) ...... def newCust (** kwargs): user name = kwargs.get ("OK", none) ID = CAPP (username) return ID DER billing (** kwargs): ID = new (** kwargs) return id ...... # Setup calling (data)  

"Itemprop =" text ">

There are several ways to perform" return "data functions:

  • Use the return statement If you have more value, then the user will get the Tupless ( returns (A, B, C) ), or decants ( returns {'customer': C, 'customer_data': D, 'Whatever': E} )

  • (In the context "works") Temporary objects have been passed by exploiting this fact (I do not like it). Which means that if a function gets a blank dotted and populates it, then the caller of that function will see the change. An important warning: If your function creates a new object with the same name, the reference is broken.

To clarify my second point: consider the function:

  def f (** kwargs): If the 'data' References for data outside the # function: data = dict (#) # New object data ['zero'] = 0 data ['zero' '] = 0 data [' Here's how it works: [14]: data [= '' [16]: data [16]: [16]: {0} in data '' zero ': 0,' zone ': 7,' 'zero [1]: 0] ' [17]: f (data = data) data {'zero' is: 0, 'zeven': 7, 'name': 'bond', 'zero_1': 0} in [18] ]: Data Out [18]: {'Zero': 0, 'Joen': 7, 'Name': 'Bond', 'zero_1': 0}  

Now, when we Create a new object, then what happens?

  def f (** kwargs): if 'data' data = dict (kwargs ['data']) # A new object was created. This is a shallow copy, which means that all its non-convertible items have been copied. # Mutable objects, on the other hand, are still # referenced # See http://stackoverflow.com/q/17246693/17523 for more details: data = dict (#) # New object data ['zero'] = [0 , [0] data ['zero'] [0] = 'xessero' data ['seven'] = 7 data ['last name'] = "bond" print ('data% s' is% repr)  

As you can see, here f creates a new object, which is a copy of the current one. Non-volatile values ​​were copied and the value of the existing code was not side-effect to f The value matching the key zero is a list (ineligible) . As a matter of fact, the value is in the context list, thus, when the copy of the dictionary was copied, this reference was also copied. This is the reason that many values ​​of the f of data were side effects only


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -