将关键字参数的默认值设置为字符串的长度

Set keyword argument's default value as length of string

我接受了重新创建一些流行的 Python 函数的挑战,其中之一是 string.count()。除了子字符串参数和起始索引(可选)之外,它还采用结束索引(也是可选的)。这是我的代码:

def count(self,substring,start=0):
    self.substring = substring
    self.start = start

    self.queue = 0
    self.counter = 0

    for a in self.string[start:]:
        if a == self.substring[0]:
            self.queue += 1
        if a == self.substring[len(self.substring) - 1] and self.queue != 0:
            self.queue -= 1
            self.counter += 1

    return self.counter

我在这个函数之外定义的一些变量:self.string,这是原始字符串本身。您可以看到我没有包含 end 参数,这就是我今天来到这里的原因。我想将参数设置为:end=len(self.string) - 1,但它只是抛出错误并说:NameError: name 'self' is not defined。有针对这个的解决方法吗?到目前为止,我可以说我的代码没有问题,因为它在没有参数的情况下工作得很好,但我可能是错的。

我认为一个简单的解决方案如下

def count(self,substring,start=0, end=None):
    if end is None:
        end = len(self.string) - 1
    self.substring = substring
    self.start = start
    self.end = end # i suppose this needs to be set

    self.queue = 0
    self.counter = 0

    for a in self.string[start:]:
        if a == self.substring[0]:
            self.queue += 1
        if a == self.substring[len(self.substring) - 1] and self.queue != 0:
            self.queue -= 1
            self.counter += 1
    
    return self.counter

尝试添加 end 为空值,然后在 def count() 的括号中添加 end = None

def count(substring, start = 0, end = None):
    # implementation omitted

后面得益于end的空值,可以在函数内部创建条件,即如果end为空值,则end为-1.

你可以写if end is None: end = len (self.string) - 1

让我们将参数分为 3 类:

  • 必需(位置)参数(如 substring
  • 可选参数(如startend),通常使用None作为无类型占位符
  • 参数与 default argument values(如 startend

另请参阅:

  • "Least Astonishment" and the Mutable Default Argument
  • Effbot/Fredrik Lundh(2008 年 7 月 17 日):Default Parameter Values in Python,通过 Wayback 存档,使用 None 作为默认值

end 参数使用切片和默认值

另见 runnable demo on IDEone:

class StringUtil:

    def __init__(self, s):
        self.string = s
    
    
    def count(self, substring, start=0, end=-1):  # end index/slice: -1 for the last, instead len(self.substring) - 1
        self.substring = substring
        self.start = start
        self.end = end
        self.queue = 0
        self.counter = 0

        for a in self.string[start:]:
            if a == self.substring[0]:
                self.queue += 1
            if a == self.substring[end] and self.queue != 0:  # -1 for the last, instead len(self.substring) - 1
                self.queue -= 1
                self.counter += 1
        
        return self.counter


sub = "o*o"
s =  "Hell" + sub + " W" + sub + "rld"
cnt = StringUtil(s).count(sub)
print(f" counted '{sub}' in '{s}' times: {cnt}")