99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

Python反射機(jī)制中參數(shù)問題如何處理?

發(fā)布時間:2018-04-11 22:50  回復(fù):0  查看:2447   最后回復(fù):2018-04-11 22:50  
Python 開發(fā)中,我們常常用到反射機(jī)制,它主要是字符串與模塊應(yīng)用之間的連接方法,核心是將字符串轉(zhuǎn)換成可以調(diào)用的模塊、模塊方法的變量。今天和大家分享的主要是反射機(jī)制中參數(shù)問題的處理相關(guān)內(nèi)容,一起來看看吧,希望對大家 學(xué)習(xí)python有所幫助。
  首先給大家介紹下 以下四個方法:
  hasattr(obj, name, /)
  Return whether the object has an attribute with the given name.
  This is done by calling getattr(obj, name) and catching AttributeError.
  hasattr() 是檢測 obj 里面是否包含了 name 屬性(函數(shù)、方法 …… ), hasattr() 返回 True 或者 False 。
  getattr(object, name[, default]) -> value
  Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
  When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case.
  getattr() 用來調(diào)用 object.name ,一般可將其賦值給其他變量。 default 是可選項(xiàng),同樣是 object.name 不存在的條件下,在不填的時候?qū)⒎祷?/span> AttributeError ;如果設(shè)置了 default 值,則返回用戶設(shè)置的 default 值。
  setattr(obj, name, value, /)
  Sets the named attribute on the given object to the specified value.
  setattr(x, 'y', v) is equivalent to “x.y = v”
  setattr() 設(shè)置一個 obj.name = value ,也就是說設(shè)置一個新的方法(函數(shù))到對象里面。
  delattr(obj, name, /)
  Deletes the named attribute from the given object.
  delattr(x, 'y') is equivalent to ``del x.y''
  delattr() 刪除 obj.name ,把對象里不需要的方法(屬性)去除。
  設(shè)置一個十分簡單的人物屬性代碼
   class  role(object):
  n = "zzh"
  date = "2018"
   def  __init__(self, name, weapon, clothes, live = 100):
  self.name = name
  self.weapon = weapon
  self.clothes = clothes
  self.live = live
   def  buy_weapon(self, weapon_name):
  print("%s buy a %s" %(self.name, weapon_name))
   def  got_headshoot(self):
  print("%s got headshot!" %self.name)
  self.live -= 50
   def  show_inner(self):
  print("Live is %s" %self.live)
   def  __del__(self):
  print("Game Over!")
  role_default = role("zzz", "AWM", "3-level")
   while  True:
  command = input('action:')
   if hasattr(role_default, command):
  command_act = getattr(role_default,command)
  command_act()
   else:
  print('Command not exists!')
  在運(yùn)行的過程中,command 輸入除了 buy_weapon 以外的字符串都是沒有問題的。
  那就來找找看buy_weapon 這個函數(shù)跟其他方法有什么不同呢?
  原來是buy_weapon 是一個帶有兩個參數(shù)的函數(shù) 'weapon_name' 。那我們來看一下報錯信息:
  TypeError: buy_weapon() missing 1 required positional argument: 'weapon_name'
  跟我們猜測的一樣,果然是少了一個參數(shù)。
  定位到代碼上,我們可以知道Error 出現(xiàn)在:
  command_act()
  也就是command_act 調(diào)用的時候缺少了參數(shù) 'weapon_name' ,于是我們想到了一個笨拙的方法(傳參 1.0 ):
   while True:
   command = input('action:')
   if hasattr(role_default,  command):
  command_act = getattr(role_default, command)
   try:
  command_act()
  except TypeError:
  factor = input('Needing a extra factor:')
  command_act(factor) elseprint('Command not exists!')
  我們只需要簡單的添加一下try 判斷,如果判斷出來 TypeError 就給他傳個參數(shù)就好了唄!
  在代碼后期的拓展中發(fā)現(xiàn),我們要傳兩個參數(shù)怎么辦??結(jié)果是我們又缺少了一個參數(shù),那如果以后要傳三個、四個、甚至一萬個參數(shù)呢?
  我們必須要找到一個方法一勞永逸地解決這個問題!首先,我們可以知道的是,所有的參數(shù)都是依賴用戶輸入的,也就是如果有一萬個參數(shù),用戶則需要輸入一萬次。
  于是我們聯(lián)想到了循環(huán),通過判斷TypeError 出現(xiàn)與否來實(shí)現(xiàn)循環(huán)輸入,只要運(yùn)行結(jié)果是 TypeError 我們就繼續(xù)輸入?yún)?shù)直到滿足函數(shù)的需求。
來源: 博客園
您還未登錄,請先登錄

熱門帖子

最新帖子

?