0

I want to make the typical implementation of palindrome function without having to specify the length of the string and its a and b as follows

def palindrome(s,a=0,b=len(s)-1):
    if a>=b:
        return True
    return s[a] == s[b] and palindrome(s,a=a+1,b=b-1)

so the user just invoke the function like that palindrome("abcba") and the b value gets computed in the first invokation of the recursion to get the value len(s)-1

5
  • 1
    The other argument names don't exist until the function is actually entered. You can do something like default b to None and then inside the function do if b is None: b=len(s) - 1 Commented Jan 31, 2020 at 18:08
  • I know this trick, I just want to see if run-time evaluation for arguments is possible Commented Jan 31, 2020 at 18:11
  • 1
    @OmarKhalid Yes, it can be done. What you need to do is define the whole function at run-time. This can be achieved by putting it inside a wrapper function, which then calls it internally and returns its result. Commented Jan 31, 2020 at 18:24
  • I know that too, I just wanted to see that If it could happen as I wanted excatly. But, apparently It can't happen. Thank you all Commented Jan 31, 2020 at 18:30
  • 1
    @OmarKhalid It is always advisable to explain what you're already considered, so as to avoid your question being closed as a duplicate. Commented Jan 31, 2020 at 20:38

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.