2

I'm trying to pass a method, specifically a method of the string class, to a function which will run it. My code looks something like:

def foo (list):
 for in_str in list[0]:
 print(in_str.list[1]())
# format of list
list = [
 [["Hello world!", "helloworld"], str.isalpha]
]

The desired operation of the function would be to invoke the isalpha method on the string in_str then print the result. This doesn't work because in_str obviously doesn't have the attribute list, but I want to know how to make list[1] reference the method.

Any suggestions?

Thanks!

asked Feb 26, 2015 at 16:43
0

2 Answers 2

3
def foo (l):
 for in_str in l[0]:
 print(l[1](in_str))
# format of list
l = [["Hello world!", "helloworld"], str.isalpha]
print(foo(l))

You need to pass a string to str.isalpha, also you have an extra pair of brackets in the list.

In [2]: foo(l)
False
True

If you just want to pass a function then pass it as a parameter and just pass a list of strings:

def foo(l, func):
 for in_str in l[0]:
 print(func(in_str))
l = ["Hello world!", "helloworld"]
print(foo(l,str.isalpha))

A nicer way may be to use map to map the func to each string:

def foo(l,func):
 return map(func,l) # list(map(...) python 3
print(foo(l,str.isalpha))
answered Feb 26, 2015 at 16:47
Sign up to request clarification or add additional context in comments.

7 Comments

Yep! That's the perfect answer. But, you say also you have an extra pair of brackets in the list, What if the OP calls the function as print(foo(l[0]))
@BhargavRao, iI think it was a typo by the OP, as using l[0], in_str `would be a list. If not we can do a little change
I'm not sure if I'm wording this properly, but what I desire the function to do is invoke in_str.isalpha() by passing it as a parameter. My actual program is much larger and has many different methods to pass, but need to be loaded by a list.
@SuperUser320, you can simply pass it as a parameter
Hmm. I think I have larger issues. When I run that in the python shell it works, yet in my program it returns [] instead of "True" or "False"
|
1

Either you call the function isalpha with a string as an argument:

str.isalpha('Hello World!")

or as an object oriented approach:

'Hello World'.isalpha()

In your case we also need to correct some indices. I also changed the variable name list because it shadows the built-in list function.

def foo(anestedlist):
 for a_str in anestedlist[0][0]:
 print(anestedlist[0][1](a_str))
# format of list
anestedlist = [[["Hello world!", "helloworld"], str.isalpha]]

and

foo(anestedlist)

prints

False
True

The important part here is

print(anestedlist[0][1](a_str))

which translates to str.isalpha(a_str), i.e. passing a string as an argument.

answered Feb 26, 2015 at 16:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.