Python decorator as a staticmethod

This is not how staticmethod is supposed to be used. staticmethod objects are descriptors that return the wrapped object, so they only work when accessed as classname.staticmethodname. Example class A(object): @staticmethod def f(): pass print A.f print A.__dict__[“f”] prints <function f at 0x8af45dc> <staticmethod object at 0x8aa6a94> Inside the scope of A, you would always … Read more

Python – Get original function arguments in decorator

The decorator login_required is passed the function (hello in this case). So what you want to do is: def login_required(f): # This function is what we “replace” hello with def wrapper(*args, **kw): args[0].client_session[‘test’] = True logged_in = 0 if logged_in: return f(*args, **kw) # Call hello else: return redirect(url_for(‘login’)) return wrapper

How to build a decorator with optional parameters? [duplicate]

I found an example, you can use @trace or @trace(‘msg1′,’msg2’): nice! def trace(*args): def _trace(func): def wrapper(*args, **kwargs): print enter_string func(*args, **kwargs) print exit_string return wrapper if len(args) == 1 and callable(args[0]): # No arguments, this is the decorator # Set default values for the arguments enter_string = ‘entering’ exit_string = ‘exiting’ return _trace(args[0]) else: … Read more

How can I pass a variable in a decorator to function’s argument in a decorated function?

You can’t pass it as its own name, but you can add it to the keywords. def decorate(function): def wrap_function(*args, **kwargs): kwargs[‘str’] = ‘Hello!’ return function(*args, **kwargs) return wrap_function @decorate def print_message(*args, **kwargs): print(kwargs[‘str’]) Alternatively you can name its own argument: def decorate(function): def wrap_function(*args, **kwargs): str=”Hello!” return function(str, *args, **kwargs) return wrap_function @decorate def … Read more

Applying a decorator to an imported function?

Decorators are just syntactic sugar to replace a function object with a decorated version, where decorating is just calling (passing in the original function object). In other words, the syntax: @decorator_expression def function_name(): # function body roughly(*) translates to: def function_name(): # function body function_name = decorator_expression(function_name) In your case, you can apply your decorator … Read more

TypeScript: remove key from type/subtraction type

Update for TypeScript 3.5: The Omit<Type, Keys> utility type is now available. Please see Mathias’ answer for an example usage. Old Answer: Since TypeScript 2.8 and the introduction of Exclude, It’s now possible to write this as follows: type Without<T, K> = { [L in Exclude<keyof T, K>]: T[L] }; Or alternatively, and more concisely, … Read more