欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python反射和內(nèi)置方法重寫操作詳解

 更新時(shí)間:2018年08月27日 15:02:02   作者:—super—  
這篇文章主要介紹了Python反射和內(nèi)置方法重寫,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python反射概念、原理及內(nèi)置方法重寫相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Python反射和內(nèi)置方法重寫操作。分享給大家供大家參考,具體如下:

isinstance和issubclass

isinstance(obj,cls)檢查是否obj是否是類 cls 的對象,類似 type()

class Foo(object):
  pass
obj = Foo()
isinstance(obj, Foo)

issubclass(sub, super)檢查sub類是否是 super 類的派生類

class Foo(object):
 pass
class Bar(Foo):
 pass
issubclass(Bar, Foo)

反射

1 什么是反射

反射的概念是由Smith在1982年首次提出的,主要是指程序可以訪問、檢測和修改它本身狀態(tài)或行為的一種能力(自?。?。這一概念的提出很快引發(fā)了計(jì)算機(jī)科學(xué)領(lǐng)域關(guān)于應(yīng)用反射性的研究。它首先被程序語言的設(shè)計(jì)領(lǐng)域所采用,并在Lisp和面向?qū)ο蠓矫嫒〉昧顺煽儭?/p>

四個(gè)反射函數(shù)

hasattr(obj,str)
檢測是否含有某屬性

getattr(obj,str)
獲取屬性,不存在報(bào)錯(cuò)

setattr(obj,str,value)
設(shè)置屬性

delattr(obj,str)
刪除屬性,不存在報(bào)錯(cuò)

導(dǎo)入其他模塊,利用反射查找該模塊是否存在某個(gè)方法

def test():
 print('from the test')

item系列

__getitem__\__setitem__\__delitem__

class Foo:
 def __init__(self,name):
  self.name=name
 def __getitem__(self, item):
  print(self.__dict__[item])
 def __setitem__(self, key, value):
  self.__dict__[key]=value
 def __delitem__(self, key):
  print('del obj[key]時(shí),我執(zhí)行')
  self.__dict__.pop(key)
 def __delattr__(self, item):
  print('del obj.key時(shí),我執(zhí)行')
  self.__dict__.pop(item)
f1=Foo('sb')
f1['age']=18
f1['age1']=19
del f1.age1
del f1['age']
f1['name']='alex'
print(f1.__dict__)

運(yùn)行結(jié)果:

del obj.key時(shí),我執(zhí)行
del obj[key]時(shí),我執(zhí)行
{'name': 'alex'}

__new__

class A:
 def __init__(self):
  self.x = 1
  print('in init function')
 def __new__(cls, *args, **kwargs):
  print('in new function')
  return object.__new__(A, *args, **kwargs)
a = A()
print(a.x)

運(yùn)行結(jié)果:

in new function
in init function
1

單例模式:

class A:
 def __new__(cls):
  if not hasattr(cls,'obj'):
   cls.obj = object.__new__(cls)
  return cls.obj
a = A()
b = A()
print(a is b)

運(yùn)行結(jié)果:

True

__call__

對象后面加括號,觸發(fā)執(zhí)行。

注:構(gòu)造方法的執(zhí)行是由創(chuàng)建對象觸發(fā)的,即:對象 = 類名() ;而對于 __call__ 方法的執(zhí)行是由對象后加括號觸發(fā)的,即:對象() 或者 類()()

class Foo:
 def __init__(self):
  pass
 def __call__(self, *args, **kwargs):
  print('__call__')
obj = Foo() # 執(zhí)行 __init__
obj()  # 執(zhí)行 __call__

運(yùn)行輸出:

__call__

__len__

class A:
 def __init__(self):
  self.a = 1
  self.b = 2
 def __len__(self):
  return len(self.__dict__)
a = A()
print(len(a))

運(yùn)行結(jié)果:

2

__hash__

class A:
 def __init__(self):
  self.a = 1
  self.b = 2
 def __hash__(self):
  return hash(str(self.a)+str(self.b))
a = A()
print(hash(a))

運(yùn)行結(jié)果:

-1777982230

__eq__

class A:
 def __init__(self):
  self.a = 1
  self.b = 2
 def __eq__(self,obj):
  if self.a == obj.a and self.b == obj.b:
   return True
a = A()
b = A()
print(a == b)

運(yùn)行結(jié)果:

True

合并名字性別一樣的人:

class Person:
 def __init__(self,name,age,sex):
  self.name = name
  self.age = age
  self.sex = sex
 def __hash__(self):
  return hash(self.name+self.sex)
 def __eq__(self, other):
  if self.name == other.name and self.sex == other.sex:return True
p_lst = []
for i in range(84):
 p_lst.append(Person('egon',i,'male'))
print(p_lst)
print(set(p_lst))

運(yùn)行結(jié)果:

[<__main__.Person object at 0x01425AB0>, <__main__.Person object at 0x01425AD0>, <__main__.Person object at 0x01425AF0>, <__main__.Person object at 0x01425910>, <__main__.Person object at 0x014258D0>, <__main__.Person object at 0x01425950>, <__main__.Person object at 0x01425970>, <__main__.Person object at 0x014259D0>, <__main__.Person object at 0x01425C70>, <__main__.Person object at 0x01425890>, <__main__.Person object at 0x01425B30>, <__main__.Person object at 0x01425BB0>, <__main__.Person object at 0x01425C30>, <__main__.Person object at 0x01429710>, <__main__.Person object at 0x01429730>, <__main__.Person object at 0x014298F0>, <__main__.Person object at 0x01429910>, <__main__.Person object at 0x01429930>, <__main__.Person object at 0x01429950>, <__main__.Person object at 0x01429970>, <__main__.Person object at 0x01429990>, <__main__.Person object at 0x014299B0>, <__main__.Person object at 0x014299D0>, <__main__.Person object at 0x014299F0>, <__main__.Person object at 0x01429A10>, <__main__.Person object at 0x01429A30>, <__main__.Person object at 0x01429A50>, <__main__.Person object at 0x01429A70>, <__main__.Person object at 0x01429A90>, <__main__.Person object at 0x01429AB0>, <__main__.Person object at 0x01429AD0>, <__main__.Person object at 0x01429AF0>, <__main__.Person object at 0x01429B10>, <__main__.Person object at 0x01429B30>, <__main__.Person object at 0x01429B50>, <__main__.Person object at 0x01429B70>, <__main__.Person object at 0x01429B90>, <__main__.Person object at 0x01429BB0>, <__main__.Person object at 0x01429BD0>, <__main__.Person object at 0x01429BF0>, <__main__.Person object at 0x01429C10>, <__main__.Person object at 0x01429C30>, <__main__.Person object at 0x01429C50>, <__main__.Person object at 0x01429C70>, <__main__.Person object at 0x01429C90>, <__main__.Person object at 0x01429CB0>, <__main__.Person object at 0x01429CD0>, <__main__.Person object at 0x01429CF0>, <__main__.Person object at 0x01429D10>, <__main__.Person object at 0x01429D30>, <__main__.Person object at 0x01429D50>, <__main__.Person object at 0x01429D70>, <__main__.Person object at 0x01429D90>, <__main__.Person object at 0x01429DB0>, <__main__.Person object at 0x01429DD0>, <__main__.Person object at 0x01429DF0>, <__main__.Person object at 0x01429E10>, <__main__.Person object at 0x01429E30>, <__main__.Person object at 0x01429E50>, <__main__.Person object at 0x01429E70>, <__main__.Person object at 0x01429E90>, <__main__.Person object at 0x01429EB0>, <__main__.Person object at 0x01429ED0>, <__main__.Person object at 0x01429EF0>, <__main__.Person object at 0x01429F10>, <__main__.Person object at 0x01429F30>, <__main__.Person object at 0x01429F50>, <__main__.Person object at 0x01429F70>, <__main__.Person object at 0x01429F90>, <__main__.Person object at 0x01429FB0>, <__main__.Person object at 0x01429FD0>, <__main__.Person object at 0x01429FF0>, <__main__.Person object at 0x01751030>, <__main__.Person object at 0x01751050>, <__main__.Person object at 0x01751070>, <__main__.Person object at 0x01751090>, <__main__.Person object at 0x017510B0>, <__main__.Person object at 0x017510D0>, <__main__.Person object at 0x017510F0>, <__main__.Person object at 0x01751110>, <__main__.Person object at 0x01751130>, <__main__.Person object at 0x01751150>, <__main__.Person object at 0x01751170>, <__main__.Person object at 0x01751190>]
{<__main__.Person object at 0x01425AB0>}

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程

希望本文所述對大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • python?中raise用法

    python?中raise用法

    這篇文章主要介紹了python?中raise用法,Python?允許我們在程序中手動(dòng)設(shè)置異常,就是使用raise?語句來實(shí)現(xiàn),下面我們就來看看raise的具體用法,文章內(nèi)容介紹詳細(xì),具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2021-12-12
  • Python實(shí)現(xiàn)求兩個(gè)csv文件交集的方法

    Python實(shí)現(xiàn)求兩個(gè)csv文件交集的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)求兩個(gè)csv文件交集的方法,涉及Python針對csv文件的讀取、遍歷、判斷等相關(guān)操作技巧,需要的朋友可以參考下
    2017-09-09
  • python爬蟲請求頭的使用

    python爬蟲請求頭的使用

    這篇文章主要介紹了python爬蟲請求頭的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Pycharm使用Database?Navigator連接mysql數(shù)據(jù)庫全過程

    Pycharm使用Database?Navigator連接mysql數(shù)據(jù)庫全過程

    這篇文章主要介紹了Pycharm使用Database?Navigator連接mysql數(shù)據(jù)庫全過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Django auth 應(yīng)用模塊詳解

    Django auth 應(yīng)用模塊詳解

    這篇文章主要介紹了Django auth 應(yīng)用模塊,auth 模塊幫助開發(fā)人員提高了工作效率,因?yàn)槊總€(gè) Web 站點(diǎn)的權(quán)限管理模塊或者用戶管理模塊基本都是相同的邏輯,Django 的 auth 模塊使開發(fā)者們不必在為一些重復(fù)性的邏輯進(jìn)行構(gòu)建,需要的朋友可以參考下
    2022-11-11
  • Python實(shí)現(xiàn)打印螺旋矩陣功能的方法

    Python實(shí)現(xiàn)打印螺旋矩陣功能的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)打印螺旋矩陣功能的方法,簡單描述了螺旋矩陣的概念、原理及Python實(shí)現(xiàn)方法,需要的朋友可以參考下
    2017-11-11
  • python數(shù)據(jù)可視化matplotlib繪制折線圖示例

    python數(shù)據(jù)可視化matplotlib繪制折線圖示例

    這篇文章主要為大家介紹了python數(shù)據(jù)可視化matplotlib繪制折線圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • python讀取LMDB中圖像的方法

    python讀取LMDB中圖像的方法

    這篇文章主要為大家詳細(xì)介紹了python讀取LMDB中圖像的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Python自動(dòng)重新加載模塊詳解(autoreload module)

    Python自動(dòng)重新加載模塊詳解(autoreload module)

    這篇文章主要介紹了Python自動(dòng)重新加載模塊詳解(autoreload module),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python實(shí)現(xiàn)移位加密和解密

    python實(shí)現(xiàn)移位加密和解密

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)移位加密和解密,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03

最新評論