Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3271652
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:44:56+00:00 2026-05-17T18:44:56+00:00

I am being passed lists of strings that I would like to convert to

  • 0

I am being passed lists of strings that I would like to convert to method calls using functools.partial:

[ 'object1.method1(arg1, arg2, 4, key=value)', 'object2.method2(6, arg1)' ...]

It’s easy to use a regexp to generate the necessary ‘func’ argument to functools.partial, but I’m having a devil of a time easily converting the string within the parens into valid *args and **kwargs to pass to functools.partial.

Every list item is guaranteed to be valid Python. I’m just unable to come up with a quick, easy way to convert strings such as ‘arg1, arg2, 4, key=value’ into somehthing functools.partial can use. What am I missing?

Update:

My appologies. I did forget vital information. The args are not valid identifiers within the scope of this procedure, hence ‘eval’ doesn’t work. They are, however, correct in the scope in which the resulting partial objects will be used, so they can be “copied” as literals.
My current procedure returns a string ‘arg1, arg2, 4, this=that’. If passed directly to functools.partial, functools.partial “wraps” it as a single string argument.

Hmmm.. the more I describe this, the more I realize that there’s no way to do this unless these identifiers are valid within this scope…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:44:57+00:00Added an answer on May 17, 2026 at 6:44 pm

    I’m writing another answer, because if I change the old one, the comments won’t reflect the the content at all. Instead, if this turns out to be a better answer, I will withdraw the other. The following should work.

    Theory of Operation:

    1. Obtain your func
    2. dissect string for arguments
    3. put every argument together with its own eval in a partial
    4. put a wrapper, func and the partial’d arguments in a partial
    5. execute in the scope needed

    The following code concentrates on 2.-4. Imports are assumed. Of course there is an additional problem of names colliding, because of the additional code in the scope

    
    def wrapper(func_, *args, **kw):
        new_args = []
        new_kw = {}
        for arg in args:
            if type(arg)==functools.partial:
                arg = arg()
            new_args.append(arg)
        for key in kw:
            value = kw[key]
            if type(value)==functools.partial:
                value = value()
            new_kw[key]=value
         return func_(*new_args, **new_kw)
    
    orig_str = 'object1.method1(arg1, arg2, 4, key=value)'
    argstr = re.search('.+\((.+)\)', orig_str).group(1)
    args = []
    kw = {}
    for x in argstr.split(','):
        if '=' in x:
            key, value = x.strip().split('=')
        else:
            value = x.strip()
            key = None            
        value = functools.partial(eval, value)
        if key:
            kw[key]=value
        else:
            args.append(value)
    
    what_you_want = functools.partial(wrapper, func, *args, **kw)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that is behaving like my variables are being passed by
I have two lists of strings that are passed into a function. They are
I'm being passed an object that returns System.Byte[*] when converted to string. This apparently
I am being passed a large chunk of XML for processing in BizTalk. The
I am being passed a set of querystring parameters within a Parameters class with
I am being passed a series of keyvaluepair<string, uint> pairs, where the string represents
showDescriptionPath is being passed from the previous Tableview. The text shows up, but the
My function is being passed a struct containing, among other things, a NULL terminated
I have a date being passed to my script in the format as yyyy-mm-dd.
I have a Uri object being passed to a constructor of my class. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.