写Java的时候,大多数都是用的驼峰格式XxxXxxXxx,转python之后,在python内部,更多的用的是下划线小写的格式xxx_xxx_xxx;然后跟外部系统交互的时候,通常又都用的是驼峰格式,因此总有地方要对驼峰和下划线的命名格式进行转换,于是,就有了下面的几行代码:
def camel_to_underline(self, camel_format):
'''
驼峰命名格式转下划线命名格式
'''
underline_format=''
if isinstance(camel_format, str):
for _s_ in camel_format:
underline_format += _s_ if _s_.islower() else '_'+_s_.lower()
return underline_format
def underline_to_camel(self, underline_format):
'''
下划线命名格式驼峰命名格式
'''
camel_format = ''
if isinstance(underline_format, str):
for _s_ in underline_format.split('_'):
camel_format += _s_.capitalize()
return camel_format
本文探讨了从Java到Python编程中命名风格的转换,包括如何将Java的驼峰式命名转换为Python的下划线式命名,反之亦然。通过几个简单的函数示例,展示了如何实现这些转换。
6万+

被折叠的 条评论
为什么被折叠?



