栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python9种方法来生成新的对象

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python9种方法来生成新的对象

先定义一个类:

1234class Point:   def __init__(self, x, y):    self.x = x    self.y = y

下面我们使用9种方法来生成新的对象:

123456789point1 = Point(1, 2)  point2 = eval("{}({}, {})".format("Point", 1, 2))  point3 = globals()["Point"](1, 2)  point4 = locals()["Point"](1, 2)  point5 = getattr(sys.modules[__name__], "Point")(1, 2)  point6 = copy.deepcopy(point1)  point7 = point1.__class__(1, 2)  point8 = type('Point', (Point, ), {})(1, 2) point9 = types.new_class('Point', (Point, ), {})(1, 2)

PS: 需要导入sys和types模块·

原文来源:https://www.pythontab.com/html/2018/pythonjichu_0523/1298.html


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/224764.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号