Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Dynamic function name
#1
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'
Quote
#2
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'}
Quote
#3
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'
Quote
#4
(Jul-11-2019, 07:18 AM)bhojendra Wrote: Sorry, I'm not getting it. Do you mean like?
Just use some_decorator instead of decorator.
Quote
#5
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'
Quote
#6
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Help | How to make Dynamic Path/Address soothsayerpg 3 184 Jul-14-2019, 03:51 PM
Last Post: DeaD_EyE
  Creating Dynamic Objects MacFie 4 328 Jun-13-2019, 02:48 PM
Last Post: MacFie
  building a dynamic INSERT between two DBs with Python3 liran 5 282 May-30-2019, 07:38 AM
Last Post: heiner55
  multi-dimm array -- dynamic PappaBear 3 243 May-03-2019, 03:47 AM
Last Post: buran
  Creation of Dynamic HTML by substituting Database values Sandy777 1 229 Apr-18-2019, 07:17 AM
Last Post: buran
  AssertionError: View function mapping is overwriting an existing endpoint function Zhavi221 7 1,209 Apr-17-2019, 01:07 PM
Last Post: Zhavi221
  Time dynamic choropleth chart - help martyl 3 490 Jan-31-2019, 11:38 PM
Last Post: martyl
  Twitter listen script, dynamic search value? quitte74 0 337 Nov-01-2018, 01:09 PM
Last Post: quitte74
  How to declare a dynamic class 'header' Alfalfa 3 494 Sep-11-2018, 02:28 PM
Last Post: Alfalfa
  Run function in parallel but inherite dynamic data from the previous function atizva 4 760 Jul-11-2018, 06:39 AM
Last Post: volcano63

Forum Jump:


Users browsing this thread: 1 Guest(s)