1

I WANNA RUN ALL THESE FUNCTIONS BY USING A FOR LOOP, BY CHANING THE FUNCTION NAME INSIDE THE FOR LOOP

# 1
def any_lowercase1(s):
     for c in s:
          if c.islower():
               return True
          else:
               return False

# 2

def any_lowercase2(s):
     for c in s:
          if 'c'.islower():
               return 'True'
          else:
               return 'False'

for i in range (2):
    func_statement = (any_lowercase+i) #I tried converting this to a string it didn't work ("any_lowercase"+str(i+1))
    print("Word 'Hello' islowercase-" , func_statement("Hello"))
    print("Word 'hello' islowercase-", func_statement("hello"))
    print("Word 'HELLO' islowercase-", func_statement("HELLO"))
    print("Word 'heLlO' islowercase-", func_statement("heLlO"))
    print("\n")

2 Answers 2

1

Python's functions are first-class objects (thanks @ShadowRanger), meaning you can pass functions as arguments to other functions, store references to them in collections, etc.

def f1():
    pass

def f2():
    pass

def f3():
    pass


list_of_functions = [f1, f2, f3]
for func in list_of_functions:
    func()

The way to reference a function is to simply not call the function (i.e., use parentheses). So if you have a function any_lowercase1, then to store a reference to that function in a sequence like a list, for example, you simply need to store the name of the function:

list_of_lowercase_funcs = [any_lowercase1, any_lowercase2]

for func in list_of_lowercase_funcs:
    print("Word 'Hello' islowercase-", func("Hello"))
    print("Word 'hello' islowercase-", func("hello"))
    print("Word 'HELLO' islowercase-", func("HELLO"))
    print("Word 'heLlO' islowercase-", func("heLlO"))
    print("\n")
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. This worked. Imagine If I had 10 functions, Still need to include mannually those 10 function names into the list_of_function, isn't it?
Yes. You could follow Bobby Ocean's approach if you want to dynamically refer to the functions, but it's unclear to me why you'd want to do that in the first place.
It's not pass-by-reference that allows this, the key phrase you meant was "Python functions are first class objects" (meaning you can pass them around, store them, etc., the same as any other object). Pass-by-reference is an unrelated concept (and technically not what Python does, though many tutorials speak imprecisely and use that term).
A shorter form of the phrase would be "Python has first-class functions".
1

The names of functions (as a string) are stored in pythons builtin vars() dictionary. In general, all variables created are placed in that dictionary by name. For example,

def f(x):
    return x

vars()['f'](5)

We first access the dictionary, then we access the item called "f", which is f. We then use that object as a function, f(5).

Comments

Your Answer

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.