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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Python 类对象及属性内置方法 classmethod、delattr、dir、hasattr、getattr、callable

發布時間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 类对象及属性内置方法 classmethod、delattr、dir、hasattr、getattr、callable 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. classmethod

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

class People(object):def __init__(self, number, name):self.number = numberself.name = namedef instance_method(self):print("This is instance method")return self@classmethoddef class_method(cls):print("This is class method")return cls@classmethoddef print_class_info(cls):print("類名為 {}".format(cls.__name__))People.class_method()
People.print_class_info()

2. delattr(object, name)

刪除對象的屬性,在不需要某個或某些屬性時,這個方法就會很有用。

In [1]: class People(object):...:     def __init__(self, number, name):...:         self.number = number...:         self.name = name...:         In [2]: wohu = People(1, "wohu")In [3]: delattr(wohu, 'number')In [4]: wohu.number
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-d950a0aab65b> in <module>
----> 1 wohu.numberAttributeError: 'People' object has no attribute 'number'In [5]: hasattr(wohu, 'number')
Out[5]: FalseIn [6]: hasattr(wohu, 'name')
Out[6]: True

3. dir([object])

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

In [7]: dir(wohu)
Out[7]: 
['__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']

4. hasattr(object, name)

判斷對象是否有某個屬性

In [5]: hasattr(wohu, 'number')
Out[5]: FalseIn [6]: hasattr(wohu, 'name')
Out[6]: True

5. getattr(object, name[, default])

獲取對象的屬性:

In [8]: getattr(wohu, 'name')
Out[8]: 'wohu'

6. callable(object)

判斷對象是否可被調用,能被調用的對象就是一個 callable 對象,比如函數 strint 等都是可被調用的。

In [9]: callable(int)
Out[9]: TrueIn [10]: callable(str)
Out[10]: True

如下代碼,默認實例化對象是不可調用的,

In [12]: class People(object):...:     def __init__(self, number, name):...:         self.number = number...:         self.name = name...:         In [13]: wohu = People(1, "wohu")In [14]: callable(wohu)
Out[14]: False

如果想要讓實例化對象能調用,如 wohu() 則需要增加 __call__ 方法:

In [15]: class People(object):...:     def __init__(self, number, name):...:         self.number = number...:         self.name = name...:     def __call__(self):...:         print("can be called")...:         print("my name is {}".format(self.name))...:         In [16]: wohu = People(1, "wohu")In [17]: callable(wohu)
Out[17]: TrueIn [18]: wohu()
can be called
my name is wohu

總結

以上是生活随笔為你收集整理的Python 类对象及属性内置方法 classmethod、delattr、dir、hasattr、getattr、callable的全部內容,希望文章能夠幫你解決所遇到的問題。

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