python类、函数举例

#类创建
class Calculator:
    def __init__(self):
        print("A calculator has been created.")
    def add(self, x, y):
        return x + y
    def subtract(self, x, y):
        return x - y
    def multiply(self, x, y):
        return x * y
cal = Calculator()
print(cal.add(2,3))
print(cal.subtract(7,2))
print(cal.multiply(3,8))

#函数
def camel_to_snake(s, separator='_'):
   result = ''
   for c in s:
       if c.isupper():
           result += separator + c.lower()
       else:
           result += c
   return result

print(camel_to_snake("ThisIsCamelCased").title()) # Output: "this_is_camel_cased"
print(camel_to_snake("ThisIsCamelCased", "-")) # Output: "this-is-camel-cased"