日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python类的实例方法必须创建对象前还是后可以调用_classmethod可以来调用类的属性,类的方法,实例化对象...

發布時間:2025/3/20 python 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python类的实例方法必须创建对象前还是后可以调用_classmethod可以来调用类的属性,类的方法,实例化对象... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

classmethod可以來調用類的屬性,類的方法,實例化對象,今天番茄加速就來講一下。

classmethod()

classmethod 修飾符對應的函數不需要實例化,不需要 self 參數,但第一個參數需要是表示自身類的 cls 參數,可以來調用類的屬性,類的方法,實例化對象等。

In [66]: class Student():

...: def __init__(self,id,name):

...: self.id = id

...: self.name = name

...: def __repr__(self):

...: return 'id = '+self.id +', name = '+self.name

...: @classmethod

...: def f(cls):

...: print(cls)

complie()

將字符串編譯成python 能識別或可以執行的代碼,也可以將文字讀成字符串再編譯。

In [74]: s = "print('helloworld')"

In [75]: r = compile(s,"", "exec")

In [76]: r

Out[76]: at 0x0000000005DE75D0, file "", line 1>

In [77]: exec(r)

helloworld

complex()

創建一個復數

In [81]: complex(1,2)

Out[81]: (1+2j)

delattr()

刪除對象的屬性

In [87]: delattr(xiaoming,'id')

In [88]: hasattr(xiaoming,'id')

Out[88]: False

dict()

創建數據字典

In [92]: dict()

Out[92]: {}

In [93]: dict(a='a',b='b')

Out[93]: {'a': 'a', 'b': 'b'}

In [94]: dict(zip(['a','b'],[1,2]))

Out[94]: {'a': 1, 'b': 2}

In [95]: dict([('a',1),('b',2)])

Out[95]: {'a': 1, 'b': 2}

dir()

不帶參數時返回當前范圍內的變量,方法和定義的類型列表;帶參數時返回參數的屬性,方法列表。

In [96]: dir(xiaoming)

Out[96]:

['__class__',

'__delattr__',

'__dict__',

'__dir__',

'__doc__',

'__eq__',

'__format__',

'__ge__',

'__getattribute__',

'__gt__',

'__hash__',

'__init__',

'__init_subclass__',

'__le__',

'__lt__',

'__module__',

'__ne__',

'__new__',

'__reduce__',

'__reduce_ex__',

'__repr__',

'__setattr__',

'__sizeof__',

'__str__',

'__subclasshook__',

'__weakref__',

'name']

divmod()

分別取商和余數

In [97]: divmod(10,3)

Out[97]: (3, 1)

總結

以上是生活随笔為你收集整理的python类的实例方法必须创建对象前还是后可以调用_classmethod可以来调用类的属性,类的方法,实例化对象...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。