以下介紹Python創(chuàng)建對(duì)象的9種方法。
classPoint:
def__init__(self,x,y):
self.x=x
self.y=yimportsysimportcopyimporttypes
point1=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)
bs:補(bǔ)充內(nèi)容
type()方法
tpye(name,bases,dict)
name類名
bases父類的元組
dict類的屬性方法和值組成的鍵值對(duì)
返回一個(gè)類對(duì)象:
__class__方法
__class__功能和type()函數(shù)一樣,都是查看對(duì)象所在的類。
以上內(nèi)容為大家介紹了Python如何創(chuàng)建對(duì)象,有哪些方法.希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。