为什么在Python函数定义中不允许使用方法的返回值作为可选参数的值?

时间:2017-03-05 03:36:54

标签: python

以下是我的代码:

def find_first_occurance(s, c, start=0, end=len(s) ):

    while start<end:
          if s[start] ==c: # whether they are the same or not
              return start
          else:
              start+=1
    return -1

print(find_first_occurance("the days make us happy make us wise","s"))

我收到了错误name "s" is not defined 我有点理解发生了什么。另一方面,如果在Python中允许这个功能会很好,对吧?

你怎么看?或者我在这里错过了什么?

1 个答案:

答案 0 :(得分:0)

定义函数时未定义 s,因此错误。

您可以尝试在函数

中初始化
def find_first_occurance(s, c, start=0, end=None ):
    if end is None:
        end = len(s)