python中self.__class__


1. python中的self

python中的self就相當於C++中的this指針
也就是指向對象本身的指針
self.name = name 就是當前對象的成員變量name賦值為name。

 

2.python的self.__class__

表示當前實例對象的類.

例如:

if hasattr(self.__class__, 'fields') and len(self.__class__.fields) > 0:

 

3. hasattr():

hasattr用於確定一個對象是否具有某個屬性。

語法:
 hasattr(object, name) -> bool
判斷object中是否有name屬性,返回一個布爾值。
>>> li=["zhangjing","zhangwei"]

>>> getattr(li,"pop")
<built-in method pop of list object at 0x011DF6C0>
>>> li.pop
<built-in method pop of list object at 0x011DF6C0>

>>> li.pop()
'zhangwei'

>>> getattr(li,"pop")()
'zhangjing'

>>>getattr(li, "append")("Moe") 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM