熱線電話:13121318867

登錄
首頁精彩閱讀python進階_淺談面向對象進階
python進階_淺談面向對象進階
2017-09-12
收藏

python進階_淺談面向對象進階

學了面向對象三大特性繼承,多態,封裝。今天我們看看面向對象的一些進階內容,反射和一些類的內置函數。

一、isinstance和issubclass 
class Foo:
 pass
 
class Son(Foo):
 pass
 
s = Son()
#判斷一個對象是不是這個類的對象,傳兩個參數(對象,類)
print(isinstance(s,Son))
print(isinstance(s,Foo))
#type更精準
print(type(s) is Son)
print(type(s) is Foo)
 
#判斷一個類是不是另一類的子類,傳兩個參數(子類,父類)
print(issubclass(Son,Foo))
print(issubclass(Son,object))
print(issubclass(Foo,object))
print(issubclass(int,object))

二、反射

反射的概念是由Smith在1982年首次提出的,主要是指程序可以訪問、檢測和修改它本身狀態或行為的一種能力(自?。?。這一概念的提出很快引發了計算機科學領域關于應用反射性的研究。它首先被程序語言的設計領域所采用,并在Lisp和面向對象方面取得了成績。

python面向對象中的反射:通過字符串的形式操作對象相關的屬性。python中的一切事物都是對象(都可以使用反射)

四個可以實現反射的函數:hasattr,getattr,setattr,delattr

下列方法適用于類和對象(一切皆對象,類本身也是一個對象)
class Foo:
 def __init__(self):
  self.name = 'egon'
  self.age = 73
 
 def func(self):
  print(123)
 
egg = Foo()
#常用:
#hasattr
#getattr
# print(hasattr(egg,'name'))
print(getattr(egg,'name'))
if hasattr(egg,'func'): #返回bool
 Foo_func = getattr(egg,'func') #如果存在這個方法或者屬性,就返回屬性值或者方法的內存地址
         #如果不存在,報錯,因此要配合hasattr使用
 Foo_func()
#不常用:
#setattr
# setattr(egg,'sex','屬性值')
# print(egg.sex)
# def show_name(self):
#  print(self.name + ' sb')
# setattr(egg,'sh_name',show_name)
# egg.sh_name(egg)
# show_name(egg)
# egg.sh_name()
 
#delattr
# delattr(egg,'name')
# print(egg.name)
 
 
# print(egg.name)
# egg.func()
# print(egg.__dict__)
 
 
#反射
#可以用字符串的方式去訪問對象的屬性、調用對象的方法
反射舉例1
class Foo:
 f = 123 #類變量
 @classmethod
 def class_method_demo(cls):
  print('class_method_demo')
 @staticmethod
 def static_method_demo():
  print('static_method_demo')
# if hasattr(Foo,'f'):
#  print(getattr(Foo,'f'))
print(hasattr(Foo,'class_method_demo'))
method = getattr(Foo,'class_method_demo')
method()
print(hasattr(Foo,'static_method_demo'))
method2 = getattr(Foo,'static_method_demo')
method2()
#類也是對象
反射舉例2 
import my_module
# print(hasattr(my_module,'test'))
# # func_test = getattr(my_module,'test')
# # func_test()
# getattr(my_module,'test')()
#import其他模塊應用反射
 
from my_module import test
 
 
def demo1():
 print('demo1')
 
import sys
print(__name__) #'__main__'
print(sys.modules)
#'__main__': <module '__main__' from 'D:/Python代碼文件存放目錄/S6/day26/6反射3.py'>
module_obj =sys.modules[__name__] #sys.modules['__main__']
# module_obj : <module '__main__' from 'D:/Python代碼文件存放目錄/S6/day26/6反射3.py'>
print(module_obj)
print(hasattr(module_obj,'demo1'))
getattr(module_obj,'demo1')()
#在本模塊中應用反射
反射舉例3
#對象
#類
#模塊 : 本模塊和導入的模塊
 
def register():
 print('register')
 
def login():
 pass
 
def show_shoppinglst():
 pass
#
print('注冊,登錄')
ret = input('歡迎,請輸入您要做的操作: ')
import sys
print(sys.modules)
# my_module = sys.modules[__name__]
# if hasattr(my_module,ret):
#  getattr(my_module,ret)()
if ret == '注冊':
 register()
elif ret == '登錄':
 login()
elif ret == 'shopping':
 show_shoppinglst()
反射舉例4  
def test():
 print('test')
三、類的內置函數
1、__str__和__repr__
class Foo:
 def __init__(self,name):
  self.name = name
 def __str__(self):
  return '%s obj info in str'%self.name
 def __repr__(self):
  return 'obj info in repr'
 
f = Foo('egon')
# print(f)
print('%s'%f)
print('%r'%f)
print(repr(f)) # f.__repr__()
print(str(f))
#當打印一個對象的時候,如果實現了str,打印中的返回值
#當str沒有被實現的時候,就會調用repr方法
#但是當你用字符串格式化的時候 %s和%r會分別去調用__str__和__repr__
#不管是在字符串格式化的時候還是在打印對象的時候,repr方法都可以作為str方法的替補
#但反之不行
#用于友好的表示對象。如果str和repr方法你只能實現一個:先實現repr
2、__del__
class Foo:
 def __del__(self):
  print('執行我啦')
 
f = Foo()
print(123)
print(123)
print(123)
#析構方法,當對象在內存中被釋放時,自動觸發執行。
#注:此方法一般無須定義,因為Python是一門高級語言,程序員在使用時無需關心內存的分配和釋放,因為此工作都是交給Python解釋器來執行,所以,析構函數的調用是由解釋器在進行垃圾回收時自動觸發執行的。
3、item系列
__getitem__\__setitem__\__delitem__
  
class Foo:
 def __init__(self):
  self.name = 'egon'
  self.age = 73
   
 def __getitem__(self, item):
  return self.__dict__[item]
 
 def __setitem__(self, key, value):
  # print(key,value)
  self.__dict__[key] = value
 
 def __delitem__(self, key):
  del self.__dict__[key]
f = Foo()
print(f['name'])
print(f['age'])
f['name'] = 'alex'
# del f['name']
print(f.name)
f1 = Foo()
print(f == f1)

4、__new__
# class A:
#  def __init__(self): #有一個方法在幫你創造self
#   print('in init function')
#   self.x = 1
#
#  def __new__(cls, *args, **kwargs):
#   print('in new function')
#   return object.__new__(A, *args, **kwargs)
# a = A()
# b = A()
# c = A()
# d = A()
# print(a,b,c,d)
 
#單例模式
class Singleton:
 def __new__(cls, *args, **kw):
  if not hasattr(cls, '_instance'):
   cls._instance = object.__new__(cls, *args, **kw)
  return cls._instance
 
one = Singleton()
two = Singleton()
three = Singleton()
go = Singleton()
print(one,two)
 
one.name = 'alex'
print(two.name)

5、__call__  
class Foo:
 def __init__(self):
  pass
 def __call__(self, *args, **kwargs):
  print('__call__')
 
obj = Foo() # 執行 __init__
obj() # 執行 __call__
Foo()() # 執行 __init__和執行 __call__
#構造方法的執行是由創建對象觸發的,即:對象 = 類名() ;而對于 __call__ 方法的執行是由對象后加括號觸發的,即:對象() 或者 類()()

6、__len__,__hash__
class Foo:
 def __len__(self):
  return len(self.__dict__)
 def __hash__(self):
  print('my hash func')
  return hash(self.name)
f = Foo()
print(len(f))
f.name = 'egon'
print(len(f))
print(hash(f))

7、__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)
 
#__eq__控制著==的結果
8、內置函數實例
class FranchDeck:
 ranks = [str(n) for n in range(2,11)] + list('JQKA')
 suits = ['紅心','方板','梅花','黑桃']
 
 def __init__(self):
  self._cards = [Card(rank,suit) for rank in FranchDeck.ranks
          for suit in FranchDeck.suits]
 
 def __len__(self):
  return len(self._cards)
 
 def __getitem__(self, item):
  return self._cards[item]
 
deck = FranchDeck()
print(deck[0])
from random import choice
print(choice(deck))
print(choice(deck))
 
紙牌游戲
class FranchDeck:
 ranks = [str(n) for n in range(2,11)] + list('JQKA')
 suits = ['紅心','方板','梅花','黑桃']
 
 def __init__(self):
  self._cards = [Card(rank,suit) for rank in FranchDeck.ranks
          for suit in FranchDeck.suits]
 
 def __len__(self):
  return len(self._cards)
 
 def __getitem__(self, item):
  return self._cards[item]
 
 def __setitem__(self, key, value):
  self._cards[key] = value
 
deck = FranchDeck()
print(deck[0])
from random import choice
print(choice(deck))
print(choice(deck))
 
from random import shuffle
shuffle(deck)
print(deck[:5])
紙牌游戲2
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 other.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))
 
#只要姓名和年齡相同就默認為一人去重
去重
以上這篇python進階_淺談面向對象進階就是小編分享給大家的全部內容了,希望能給大家一個參考


數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢