I have to use dynamic function name which is defined after some decorator:
@some_decorator
def dynamic():
return 'somethin'
Something like:
some_var = 'value'
@some_decorator
def f'dynamic_{some_var}'(): # syntax error, but if it was available, would have been a nice feature
return 'somethin'
You can do something like this:
def create_func(name):
def myfunc(*args, **kwargs):
print(args, kwargs)
# write function content here
return myfunc
func_name = 'mys'
decorator = lambda x: x # entity decorator
globals()[func_name] = decorator(create_func(func_name))
mys(3,4, name='sample')
Output:
(3, 4) {'name': 'sample'}
Sorry, I'm not getting it. Do you mean like?
decorator = lambda x: some_decorator
BTW, I don't have to call the function. I just have to define the function.
@some_decorator
def dynamic..():
return 'somethin'
Ah, yeah. I meant that. But didn't work for me. It's actually like:
decorator = lambda x: some_decorator.some_method()
I expected it work. But it din't.
Also, tried:
some_decorator = lambda x: x.some_method()
Where, my actual function definition is like:
@some_decorator.some_method()
def dynamic():
return 'somethin'
If your some_decorator
function is already defined, you don't need to use the lambda at all. Drop line # 8 in my code above and just use some_decorator
instead of decorator
.