假设我想检查一个字符串是否包含列表中的任何字符串。一种方法是
word = 'hello world'
any(substring in word for substring in ['hello','apple'])
虽然这对我来说有点冗长。理想情况下我想要
word.ContainsAny('hello','apple')
我读到过这样一个针对内置对象的扩展方法可以通过forbiddenfruit包来实现,但我不知道如何实现。有人能给我看看,或者建议更简洁的方法吗?你知道吗
编辑:我知道我可以编写如下函数:
ContainsAny(word,['hello','apple'])
但是扩展方法在我看来更整洁。你知道吗
似乎对禁果很直接:
我真的同意这是个糟糕的主意。引用禁果自述:
1。子类示例
2。禁果示例
来自评论:
https://stackoverflow.com/a/15975791/10552105
好吧,您可以将
str
子类化:但有疑问的是,为什么要创建一个子类来访问单个方法。你知道吗
相关问题 更多 >
编程相关推荐