fromfunc functions

A

azrael

I came across the fromfunc() function in numpy where you pass as an
argument the name of a function as a string and also the atributes for
the desired function.

I find this extremly usefull and sexy. Can someone point me how write
a function of such capabilities
 
C

Chris

I came across the fromfunc() function in numpy where you pass as an
argument the name of a function as a string and also the atributes for
the desired function.

I find this extremly usefull and sexy. Can someone point me how write
a function of such capabilities

class test_class():
def test_func(self, string):
print string

test = test_class()
getattr(test, 'test_class'){'this is the test argument')

getattr raises an Attribute Error though if the function doesn't exist
so you'll need to catch that.
 
M

Marc 'BlackJack' Rintsch

I came across the fromfunc() function in numpy where you pass as an
argument the name of a function as a string and also the atributes for
the desired function.

If you mean `fromfunction()` then you don't give the name of the function
as string but the function itself as argument. And what you call
attributes are arguments.
I find this extremly usefull and sexy. Can someone point me how write
a function of such capabilities

Functions are objects in Python, you can pass them around like any other
object/value.

In [18]: def f(func, arg):
....: return func(arg)
....:

In [19]: f(int, '42')
Out[19]: 42

In [20]: f(str.split, 'a b c')
Out[20]: ['a', 'b', 'c']

Ciao,
Marc 'BlackJack' Rintsch
 
C

Chris

I came across the fromfunc() function in numpy where you pass as an
argument the name of a function as a string and also the atributes for
the desired function.

I find this extremly usefull and sexy. Can someone point me how write
a function of such capabilities

getattr(object, 'function name')(*args)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top