python 魔術方法

來源:互聯網
上載者:User

標籤:

import timefunc_count_time_dict = {}def count_time(func):    global func_count_time_dict    func_count_time_dict[func]=[1,time.localtime()]    def wrapper():        global func_count_time_dict        func()        print ‘func name: ‘,func.__name__        print ‘count: ‘,func_count_time_dict[func][0]        print ‘last call time: ‘,time.asctime(func_count_time_dict[func][1])                func_count_time_dict[func][0]+=1        func_count_time_dict[func][1]=time.localtime()        return wrapper@count_timedef foo():    print ‘in foo()‘ @count_timedef foo_1():    print ‘in foo_1‘foo_1()foo_1()foo_1()    foo()foo_1()foo()foo()foo_1()

 

class MyIterator(object):      def __init__(self, data_iter):          self.data_iter = data_iter          self.start = 0      def __iter__(self):          return self        def next(self):            print self.start        if self.start >= len(self.data_iter):              raise StopIteration          ans = self.data_iter[self.start]        self.start += 1        return ans        if __name__ == "__main__":      iter = MyIterator(range(10))      for i in iter:          print i  

 

def fib(max):      a, b = 0, 1                while a < max:          yield a                    a, b = b, a + b          for i in fib(1000):      print(i)  

 

map_output = map(lambda x: 2 * x, range(10))print map_outputprint map(lambda x, y : x + y, range(8), range(8)) reduce_output = reduce(lambda x, y:x * y, range(1, 10))##pep8?print reduce_outputm=lambda x,y:x>yprint m(1,5)filter_output = filter(lambda x:x % 2,  range(1, 10))print filter_output

 

python 魔術方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.