python - 使用类 python 组织类似的方法
问题描述
我只是想知道如何将 python 类中的类似方法分组。这是一个例子:
class Test:
def add(a, b):
return a + b
def sub(a, b):
return a - b
我想对这些函数进行分组,以便您可以这样称呼它们
me = Test
print(me.math.add(5, 2))
print(me.math.sub(5, 2))
如果这是可能的。在这种情况下,我将这些函数与称为数学的东西组合在一起。我什至不确定你是否可以这样做,但它会很方便
解决方案
这行得通吗?
class Test:
def __init__(self):
self.math = Math()
class Math:
def add(self, a, b):
return a + b
def sub(self, a, b):
return a - b
test = Test()
print(test.math.add(1, 3))
print(test.math.sub(6, 3))
推荐阅读
- c# - 如何提高数据库查询的性能?
- python - 使用 BeautifulSoup 查找与特定关键字相关的链接
- video - 使用 FFMPEG 和 GPU 将 H265 转换为 MP4
- python - 从列表创建字典
- sql - SQL:跨表分区的 CROSS JOIN
- dart - Flutter - 显示抽屉按钮而不是后退按钮
- asp.net - IISExpress - 让网站识别它自己的 SSL 端口绑定
- react-native - 世博会应用程序中的 Pusher chatKit onMessage 挂钩失败
- android - MapBox Navigation SDK 转弯方向未公布
- vba - 虚假输入参数值消息