尽管如此我认为这种检查很常见,并且在标准Python中使用它是值得的。 任何反应,评论? Michele I often feel the need to extend the string method ".endswith" to tuplearguments, in such a way to automatically check for multiple endings.For instance, here is a typical use case: if filename.endswith((''.jpg'',''.jpeg'',''.gif'',''.png'')):print "This is a valid image file" Currently this is not valid Python and I must use the ugly if filename.endswith(''.jpg'') or filename.endswith(''.jpeg'') \or filename.endswith(''.gif'') or filename.endswith(''.png''):print "This is a valid image file" Of course a direct implementation is quite easy: import sys class Str(str):def endswith(self,suffix,start=0,end=sys.maxint):#not sure about sys.maxintendswith=super(Str,self).endswithif isinstance(suffix,tuple):return sum([endswith(s,start,end) for s in suffix]) # multi-orreturn endswith(suffix,start,end) if Str(filename).endswith((''.jpg'',''.jpeg'',''.gif'',''.pn g'')):print "This is a valid image file" nevertheless I think this kind of checking is quite common and it would beworth to have it in standard Python. Any reaction, comment ?Michele推荐答案 Michele Simionato写道: Michele Simionato wrote: 我经常觉得需要扩展字符串方法.endswith以这种方式自动检查多个结尾。例如,这是一个典型的用例: 如果filename.endswith(('''。 jpg'',''。jpeg'',''。gif'',''。png'')):打印这是一个有效的图像文件 目前这不是有效的Python,我必须使用丑陋的 如果filename.endswith(''。jpg'')或filename.endswith(''。jpeg'')\ 或filename.endswith(''。gif'')或filename.endswith(''。png''): print"这是一个有效的图像文件 当然是直接的实现很简单: 导入sys 类Str(str): def endswith(self,suffix,start = 0,end = sys.maxint) :#不确定 sys.maxint endswith = super(Str,self).endswith if isinstance(suffix,tuple): return sum([endswith(s, s开头,结尾)后缀为s)#multi-or return endswith(后缀,开头,结尾) 如果Str(文件名).endswith((''。jpg'',''。jpeg'',''。gif'',''。 pn g'')):打印这是一个有效的图像文件 然而我认为这种检查很常见,值得拥有它在标准的Python中。 I often feel the need to extend the string method ".endswith" to tuple arguments, in such a way to automatically check for multiple endings. For instance, here is a typical use case: if filename.endswith((''.jpg'',''.jpeg'',''.gif'',''.png'')): print "This is a valid image file" Currently this is not valid Python and I must use the ugly if filename.endswith(''.jpg'') or filename.endswith(''.jpeg'') \ or filename.endswith(''.gif'') or filename.endswith(''.png''): print "This is a valid image file" Of course a direct implementation is quite easy: import sys class Str(str): def endswith(self,suffix,start=0,end=sys.maxint):#not sure about sys.maxint endswith=super(Str,self).endswith if isinstance(suffix,tuple): return sum([endswith(s,start,end) for s in suffix]) # multi-or return endswith(suffix,start,end) if Str(filename).endswith((''.jpg'',''.jpeg'',''.gif'',''.pn g'')): print "This is a valid image file" nevertheless I think this kind of checking is quite common and it would be worth to have it in standard Python. 我喜欢这个功能请求。 如果endswith的参数不是字符串, 它应该尝试将参数视为列表或元组。 thomas Hi, I like this feature request. if the argument to endswith is not a string,it should try to treat the argument as a list or tuple. thomas On Fri,2003年7月18日上午05:01:47 -0700,Michele Simionato写道:On Fri, Jul 18, 2003 at 05:01:47AM -0700, Michele Simionato wrote:我经常觉得需要扩展字符串方法" .endswith"以这种方式自动检查多个结尾。例如,这是一个典型的用例: 如果filename.endswith(('''。 jpg'',''。jpeg'',''。gif'',''。png'')):打印这是一个有效的图像文件 目前这不是有效的Python,我必须使用丑陋的 如果filename.endswith(''。jpg'')或filename.endswith(''。jpeg'')\ 或filename.endswith(''。gif'')或filename.endswith(''。png''): print"这是一个有效的图像文件" I often feel the need to extend the string method ".endswith" to tuple arguments, in such a way to automatically check for multiple endings. For instance, here is a typical use case: if filename.endswith((''.jpg'',''.jpeg'',''.gif'',''.png'')): print "This is a valid image file" Currently this is not valid Python and I must use the ugly if filename.endswith(''.jpg'') or filename.endswith(''.jpeg'') \ or filename.endswith(''.gif'') or filename.endswith(''.png''): print "This is a valid image file" extensions =(''.jpg'',''。jpeg'',''。gif'',''。png'') if filter(filename.endswith,扩展): 打印这是一个有效的图像文件 Jp - Pascal是Pascal,Pascal是狗肉。 - M. Devine和P. Larson,计算机科学340 extensions = (''.jpg'', ''.jpeg'', ''.gif'', ''.png'')if filter(filename.endswith, extensions):print "This is a valid image file Jp --"Pascal is Pascal is Pascal is dog meat."-- M. Devine and P. Larson, Computer Science 340 Michele>我经常觉得需要扩展字符串方法.endswith Michele>以这种方式自动检查 Michele>来组合参数。多重结局。例如,这是一个典型的用例: Michele> if filename.endswith((''。jpg'',''。jpeg'',''。gif'',''。png'')): Michele>打印这是一个有效的图像文件 这类似于实例的工作方式,其第二个arg可以是一个类别或类型或类型包含类和类型的元组。 我建议您向SF提交功能请求。 stringobject.c和 unicodeobject.c的补丁将有助于提高接受的机会,对于对称性 你也应该修改两种类型的startswith方法。 /> 跳过 Michele> I often feel the need to extend the string method ".endswith"Michele> to tuple arguments, in such a way to automatically check forMichele> multiple endings. For instance, here is a typical use case: Michele> if filename.endswith((''.jpg'',''.jpeg'',''.gif'',''.png'')):Michele> print "This is a valid image file" This is analogous to how isinstance works, where its second arg can be aclass or type or a tuple containing classes and types. I suggest you submit a feature request to SF. A patch to stringobject.c andunicodeobject.c would help improve chances of acceptance, and for symmetryyou should probably also modify the startswith methods of both types. Skip 这篇关于功能要求:更好的str.endswith的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 16:13