python9种方法来生成新对象

先定义一个类:

class Point():

  def __init__(self, x, y):

      self.x = x

      self.y = y

生成新对象:

1、point = Point(1, 2)

2、point = eval("{},({},{})".format("Point", 1,2))

3、point = globals()["Point"](1, 2)

4、point = locals()["Point"](1, 2)

5、point = getattr(sys.modules[__name__], "Point")(1, 2)

6、point = copy.deepcopy(point)

7、point = point.__class__(1, 2)

8、point = type("Point", (Point, ),{})(1, 2)

9、point = type.new_class("Point", (Point, ),{})(1, 2)



你可能感兴趣的:(python9种方法来生成新对象)