Python equivalents to MATLAB str2func, func2str, ischar, isfunc?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dmitrey

    #1

    Python equivalents to MATLAB str2func, func2str, ischar, isfunc?

    I can't find these via web serch

    thank you in advance,
    Dmitrey

  • Alexander Schmolck

    #2
    Re: Python equivalents to MATLAB str2func, func2str, ischar, isfunc?

    "dmitrey" <openopt@ukr.ne twrites:
    I can't find these via web serch
    >
    thank you in advance,
    Dmitrey

    str2func: getattr(some_mo dule, 'f')
    func2str: f.__name__
    ischar: isinstance(x, basestring) and len(x) == 1
    isfunc: callable(x) # is most likely to be what you want

    'as

    Comment

    • Travis Oliphant

      #3
      Re: Python equivalents to MATLAB str2func, func2str, ischar, isfunc?

      dmitrey wrote:
      I can't find these via web serch
      >
      You won't find exact equivalents. But, the same functionality is
      available. Perhaps you would like to show us what you are trying to do
      in Python.

      Python's eval has some similarity with str2func

      Python's repr() or str() has some similarity with func2str

      ischar(A) is similiar to isinstance(A, str)

      isfunc is similiar to callable

      -Travis

      P.S. (if you are using NumPy, then there are other possibilities as well.

      Comment

      • dmitrey

        #4
        Re: Python equivalents to MATLAB str2func, func2str, ischar, isfunc?

        Thank you
        (however in MATLAB ischar is the same as isstr)
        but what if I don't know the name of module?
        I.e. I have

        def myfunc(param): ...
        #where param can be both funcName or a function, and I want to obtain
        both name and func, something like
        if isinstance(para m, basestring):
        func, funcName = <something>, param
        else: func, funcName = param, param.__name__
        what should I type instead of <something>?

        D.



        On Mar 14, 7:06 pm, Alexander Schmolck <a.schmo...@gma il.comwrote:
        "dmitrey" <open...@ukr.ne twrites:
        I can't find these via web serch
        >
        thank you in advance,
        Dmitrey
        >
        str2func: getattr(some_mo dule, 'f')
        func2str: f.__name__
        ischar: isinstance(x, basestring) and len(x) == 1
        isfunc: callable(x) # is most likely to be what you want
        >
        'as

        Comment

        • Alexander Schmolck

          #5
          Re: Python equivalents to MATLAB str2func, func2str, ischar, isfunc?

          "dmitrey" <openopt@ukr.ne twrites:
          Thank you
          (however in MATLAB ischar is the same as isstr)
          Right, sorry.
          but what if I don't know the name of module?
          I.e. I have
          >
          def myfunc(param): ...
          #where param can be both funcName or a function, and I want to obtain
          both name and func, something like
          if isinstance(para m, basestring):
          func, funcName = <something>, param
          else: func, funcName = param, param.__name__
          what should I type instead of <something>?
          globals()[param] (I assume you don't want a local function, should one exist?)

          'as

          Comment

          Working...