<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      python技巧31[python Tip2]

       

      1 函數的默認值為mutable類型時的問題和解決辦法

      def f2(a, L=[]):
          L.append(a)
          
      return L
      print(f2(1))
      print(f2(2))
      print(f2(3))
      def f3(a, L=None):
          
      if L is None:
              L 
      = []
          L.append(a)
          
      return L
      print(f3(1))
      print(f3(2))
      print(f3(3))
      # the result will be
      #
      [1]
      #
      [1, 2]
      #
      [1, 2, 3]
      #
      [1]
      #
      [2]
      #
      [3]
       
      from copy import deepcopy
      def resetDefaults(f):
          defaults 
      = f.__defaults__
          
      def resetter(*args, **kwds):
              f.
      __defaults__ = deepcopy(defaults)
              
      return f(*args, **kwds)
                          
          resetter.
      __name__ = f.__name__
          
      return resetter
                                  
      @resetDefaults 
      # This is how you apply a decorator 
      def TestDefaultCorrect(item, stuff = []): 
           stuff.append(item) 
           
      print (stuff)

      TestDefaultCorrect(
      1)
      # prints '[1]' 
      TestDefaultCorrect(2)
      # prints '[2]', as expected

       

       

      2 函數裝飾模式

      def decorator1(func):
          
      return lambda: func() + 1

      def decorator2(func):
          
      def print_func(): 
              
      print (func())
          
      return print_func

      @decorator2
      @decorator1
      def function():
          
      return 41

      # to cal functions(), it is equal to call decorator2(decorator1(function))

      function()
      # prints '42'

       

       

      3 檢查類型的屬性和方法是否存在

      class Class:
          answer 
      = 42

      getattr(Class, 
      'answer')
      # returns 42
      getattr(Class, 'question''What is six times nine?')
      # returns 'What is six times nine?'
      getattr(Class, 'question')
      # raises AttributeError

       

       

      4 動態修改類中的函數

      class Class: 
         
      def method(self): 
             
      print ('Hey a method' )
             
      instance 
      = Class() 
      instance.method() 
      # prints 'Hey a method' 

      def new_method(self):
          
      print ('New method wins!')
          
      Class.method 
      = new_method
      instance.method()
      # prints 'New method wins!'

       

       

      5 類的靜態方法的使用

      class Class:
          @classmethod
          
      def a_class_method(cls): 
             
      print ('I was called from class %s' % cls)

          @staticmethod
          
      def a_static_method(): 
              
      print ('I have no idea where I was called from')
              
          
      def another_static_method():
               
      print ('I have no idea where I was called from2')

          
      def an_instance_method(self):
              
      print ('I was called from the instance %s' % self)

      instance 
      = Class()

      Class.a_class_method()
      instance.a_class_method()
      # both print 'I was called from class __main__.Class'

      Class.a_static_method()
      instance.a_static_method()
      # both print 'I have no idea where I was called from'

      Class.another_static_method()
      # both print 'I have no idea where I was called from2'
      #
      instance.another_static_method()
      #
      TypeError: another_static_method() takes no arguments (1 given)

      #Class.an_instance_method()
      #
       TypeError: an_instance_method() takes exactly 1 positional argument (0 given)
      instance.an_instance_method()
      # prints something like 'I was called from the instance <__main__.Class instance at 0x2e80d0>'

       

      6 使用main來作為python文件的入口

      if __name__ == "__main__":

       

      7 將函數的輸出重定向到文件 (以下代碼有個bug)

      import sys

      def stdoutToFile(filename, function, args ):
          oldStdout 
      = sys.stdout
          f 
      = open(filename, "w" )
          sys.stdout 
      = f 
          function(args)
          
      #sys.stdout.flush()
          #f.close()
          sys.stdout = oldStdout


      if __name__=='__main__':
        
      print("modules")
        stdoutToFile(
      "modules.txt", help, "modules")
        
      print("builtins")
        stdoutToFile(
      "builtins.txt", help, "builtins")
        
      print("keywords")
        stdoutToFile(
      "keyword.txt", help, "keywords")

       

       

       

      參考:http://www.siafoo.net/article/52#id26

       

      完!

      posted @ 2011-01-14 19:27  iTech  閱讀(1279)  評論(2)    收藏  舉報
      主站蜘蛛池模板: 国产一区二区a毛片色欲| 亚洲一本二区偷拍精品| 亚洲精品电影院| 亚洲爆乳WWW无码专区| 一本高清码二区三区不卡| 一级片免费网站| 国产桃色在线成免费视频| 久久久久人妻一区二区三区 | 自拍视频亚洲精品在线| julia无码中文字幕一区| 欧美日韩中文字幕久久伊人| 国产又色又爽又刺激在线观看| 国产午夜91福利一区二区| 97人人模人人爽人人少妇| 亚洲一级特黄大片在线观看| 常宁市| 一区天堂中文最新版在线| 亚洲国产美女精品久久久| 日本阿v片在线播放免费| 国产精品一二三中文字幕| 二区三区国产在线观看| 日韩精品无码去免费专区 | 韩国V欧美V亚洲V日本V| 亚洲精品香蕉一区二区| 亚洲国产精品综合久久网络| 乱人伦中文字幕成人网站在线 | 国产精品一区二区麻豆蜜桃| 亚洲韩国精品无码一区二区三区 | 午夜福利在线观看6080| 色婷婷日日躁夜夜躁| 麻豆国产AV剧情偷闻女邻居内裤| 欧美黑人又粗又大又爽免费| 国产老熟女国语免费视频| 久9re热视频这里只有精品免费| 国产极品美女高潮抽搐免费网站| 人妻少妇偷人精品免费看| 日本高清在线观看WWW色| A三级三级成人网站在线视频 | 亚洲婷婷综合色香五月| 天堂一区二区三区av| 国产精品午夜福利导航导|