Wednesday 15 January 2014

python - decorate a Python3 function to ignore *args in (a, b, *args, **kwargs)? -


Python 3.3: Can I decorate a function to ignore all fundamental arguments that do not match the nominal parameters? > import functools def ignore_star_args (): def wrapper (function_): @ functools.wraps ( Function_ wrap define (* f_args): return function_ (/ * only named args / /) wrapped return wrapper @ignore_star_args () def list_args (a, b, * args): # two positional arguments return [a, b] + List (args) & gt; & Gt; & Gt; List_args (1, 2, "non-positional arg") [1, 2]

I think you are misguiding about the conditions you are using.

The values ​​passed to one are of the keyword without the argument of the position

There are three different types of positional criteria; Include the * parameter or bare * (if any). For example, in this code: (F, A, B, C = 3, D = 4, * ARGE, E, F = 6, ** KW) DEGF FU (1, 2, D = 3, C = 4, e = 5, g = 7)

a , b , c < / Code> and d are all position-or-keyword parameters ; arguments is an var-positional parameter ; e and f keywords are only parameters ; And kw is an var-keyword parameter . (There is no example of the positional-only parameter , because they can not appear in the function definition; You need to type c / java / .net / rpithon / whatever extension code, or is in the call, 1 is in the call, the code is manually, to get them.)

Positional arguments ; 3 , 4 , 5 , and 7 are keyword logic ; d , c , e , and g is keyword identifier .

Python will match two positional arguments in the first two local-only or positional-or-keyword parameters, or if they are not enough, they will match the positational logic, If the names are not found for keyword or keyword-only parameters with the same names or var-keyword arguments, then inside foo :

  a, b, c, D, args, e, f, kw = 1, 2, 4, 3, (), 5, 6, {'g': 7}   

Now when you have all this straight I have some I have to face: Sometimes the use of Python different , instead of "argument", "formal argument" rather than "argument" and "formal argument" you mostly mostly the source code oldest It looks deep in parts, but sometimes it bubbles at a higher level.


So, what you asked in other words, you want to accept but ignore keyword arguments. It's easy: just accept them with a var-keyword parameter, and do not pass through them:

  def wrapped (* args, ** kwargs): return function _ (* F_args)   

However, I think that you wanted to ask how to ignore all arguments, positional or keyword, which Does not match position-only or positional status-that keyword parameter

To do this, you must inspect the signature of the wrapped function.

What kinds of different types of members can be difficult to find, and when you do, then can you have difficulty understanding ?? But fortunately, the documentation gathers everyone in a convenient chart.

You can see that there is nothing like this in the function that looks like its parameter signature? | But there is a code object in it, which does of course, the code object was designed back when there were only 2 types of criteria in Python, and they were called "formal arguments" rather than "parameters" So it can not immediately be clear what you want here, co_argcount one but believe me, it is so.

Such:

  def wrapped (* args): return function _ (* (args [: function_ __ code __. Call_arkaccount]))   

What you want to do with keyword arguments (remember, you can match the designated position-or-keyword or positional-only parameters!), You can ** kw And want to do anything with co_versions .

No comments:

Post a Comment