469,362 Members | 2,546 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

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

I can't find these via web serch

thank you in advance,
Dmitrey

Mar 14 '07 #1
4 7880
"dmitrey" <op*****@ukr.netwrites:
I can't find these via web serch

thank you in advance,
Dmitrey

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

'as
Mar 14 '07 #2
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.

Mar 14 '07 #3
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(param, 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...@gmail.comwrote:
"dmitrey" <open...@ukr.netwrites:
I can't find these via web serch
thank you in advance,
Dmitrey

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

'as

Mar 14 '07 #4
"dmitrey" <op*****@ukr.netwrites:
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(param, 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
Mar 14 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Carl | last post: by
3 posts views Thread by Ali | last post: by
4 posts views Thread by dataangel | last post: by
19 posts views Thread by Nicolas Pernetty | last post: by
53 posts views Thread by Michael Tobis | last post: by
9 posts views Thread by Carl | last post: by
4 posts views Thread by wang frank | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.