99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python專用方法與迭代機(jī)制實例分析
Python專用方法與迭代機(jī)制實例分析
2017-11-23
收藏

Python專用方法與迭代機(jī)制實例分析

本文實例講述了Python專用方法與迭代機(jī)制,分享給大家供大家參考之用。具體分析如下:

眾所周知,Python 設(shè)計哲學(xué)是“優(yōu)雅”、“明確”、“簡單”,對于一件事只用一種最好的方法來做,而這種優(yōu)雅在于背后很自然的隱藏了很多細(xì)節(jié)。比如對一些對象直接用for 語句來迭代,一些全局函數(shù)可以作用于很多具有共同特征的對象,還有生成器裝飾器自省等特性。其中很多實現(xiàn)都是借助 Python  內(nèi)部專用方法,而對外則使用統(tǒng)一的全局函數(shù)來進(jìn)行操作,在配合一些語法糖,使得 Python 寫起來愈發(fā)的方便,符合人的直覺。

Python 專用方法
類的私有方法:以雙線劃線開頭,但是不以雙下劃線結(jié)尾的方法;
類的專有方法:以雙下劃線開頭和結(jié)尾,常用來被內(nèi)建函數(shù)調(diào)用;
模塊私有對象:以單下劃線開頭,不能被導(dǎo)入到其他的模塊中去;    
#!/usr/bin/env python
# Python3 實現(xiàn)
_modeluprivate = '本模塊私有'  #不能用 from module import * 導(dǎo)入
 
class People():
  def __myprivate(self):
    print("This is a private fun")
  def __test__(self):
    print('call __private: ',end='')
    self.__myprivate()
 
if __name__ == '__main__':
  a = People()
  a.__test__()      # 專有方法,一般系統(tǒng)專用,自己的類方法不要用這種新式命名
  a._People__myprivate() # 私有方法被對外被翻譯成了這種名字,從而達(dá)到私有的效果
  print(_modeluprivate)
    
'''''
輸出
call __private: This is a private fun
This is a private fun
本模塊私有
'''

Python 迭代機(jī)制

Python 中的可迭代對象是實現(xiàn)了 __iter__() 方法的對象,而 __iter__() 方法返回一個迭代器對象,迭代器對象內(nèi)部要實現(xiàn) __next__() 方法。迭代器對外提供了一個統(tǒng)一的遍歷集合的接口,并且可以直接用 for 語句來進(jìn)行操作,非常的方便。對于一些特別大甚至無限的集合,迭代器避免了一次性將數(shù)據(jù)集載入,幾乎是唯一的訪問方法。    
#!/usr/bin/env python
# Python3 實現(xiàn)
class IterTest():
  def __init__(self):
    self.a = 0
  def __iter__(self):
    return self
  def __next__(self):
    self.a += 1
    if self.a > 3:
      raise StopIteration
    return self.a
 
if __name__ == '__main__':
  a = IterTest()
  for i in a:
    print(i,end=' ')
  b = IterTest()
  print(list(b)) # list()構(gòu)造器,可以接受可迭代對象
  c = IterTest()
  print(next(c), next(c), next(c))
 
'''''
輸出
1 2 3 [1, 2, 3]
1 2 3
'''

Python 的生成器其實返回的也是一個迭代器,同樣可以對其使用 next() 函數(shù),對其使用 for操作,有了 yield 關(guān)鍵字使得創(chuàng)建生成器更加的方便。    
#!/usr/bin/env python
# Python3 實現(xiàn)
def funGenerate():
  yield 1
  yield 2
  yield 3
 
if __name__ == '__main__':
  a = funGenerate()
  for i in a:
    print(i, end=' ')
  b = funGenerate()
  print(next(b),next(b),next(b))
 
'''''
輸出
1 2 3 1 2 3
'''
希望本文所述對大家Python程序設(shè)計的學(xué)習(xí)有所幫助。

數(shù)據(jù)分析咨詢請掃描二維碼

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

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }