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

Omar Khalid
  • 324
  • 1
  • 3
  • 15
  • 1
    [*The other argument names don't exist until the function is actually entered.*](https://stackoverflow.com/a/4575347/5858851) You can do something like default `b` to `None` and then inside the function do `if b is None: b=len(s) - 1` – pault Jan 31 '20 at 18:08
  • I know this trick, I just want to see if run-time evaluation for arguments is possible – Omar Khalid Jan 31 '20 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. – ekhumoro Jan 31 '20 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 – Omar Khalid Jan 31 '20 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. – ekhumoro Jan 31 '20 at 20:38

0 Answers0