小碼哥

          誰謂河廣,一葦杭之

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            7 隨筆 :: 17 文章 :: 74 評(píng)論 :: 0 Trackbacks

          常用鏈接

          留言簿(21)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          訂閱Canvas

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          以教程中的一段小詩開始:
          Programming is fun
          When the work is done
          if you wanna make your work also fun:
              use Python!

           1 #!/usr/bin/env python
           2 #coding=utf-8
           3 poem = '''\
           4 Programming is fun
           5 When the work is done
           6 if you wanna make your work also fun:
           7     use Python!
           8 '''
           9 
          10 = file('c:\\poem.txt','w')
          11 f.write(poem)
          12 f.close()

          哇,下面這段真是解釋性的腳本語言的魅力啊,動(dòng)態(tài)生成語句,然后直接解釋執(zhí)行,太靈活了

          1 #!/usr/bin/env python
          2 #coding=utf-8
          3 stm = 'a = 10'
          4 exec(stm)
          5 print a

          直接輸出了a的值10

          還有,更厲害的是可以給對(duì)象動(dòng)態(tài)的添加屬性,不知道這么理解對(duì)不對(duì)

           1 #!/usr/bin/env python
           2 #coding=utf-8
           3 class Person:
           4     def __init__(self, tel, mobile, email, address, **elseInfo):
           5         self.tel = tel
           6         self.mobile = mobile
           7         self.email = email
           8         self.address = address
           9         
          10         for key, value in elseInfo.items():
          11             stm = "self.%s = \"%s\"" % (key, value)
          12             exec(stm)
          13 
          14 loh = Person('2*******3','1368****533','elgnaw(at)tju.edu.cn','Tianjin University',email2 = 'burgundy.loh(at)gmail.com',mobile2 = '136*****211')
          15 
          16 print loh.email
          17 print loh.email2
          18 print dir(loh)
          19 

          輸出為

          elgnaw(at)tju.edu.cn
          burgundy.loh(at)gmail.com
          ['__doc__', '__init__', '__module__', 'address', 'email', 'email2', 'mobile', 'mobile2', 'tel']

          這里是python的教程:簡明 Python 教程
          也開始你的python之路吧
          posted on 2009-03-25 09:02 小碼哥 閱讀(310) 評(píng)論(0)  編輯  收藏 所屬分類: python
          主站蜘蛛池模板: 万年县| 民和| 资源县| 玉屏| 绥滨县| 葫芦岛市| 宿迁市| 朝阳区| 巴青县| 兴文县| 上饶市| 家居| 翁源县| 诏安县| 博客| 枣阳市| 林西县| 龙江县| 雷波县| 鸡西市| 南川市| 库尔勒市| 扎赉特旗| 纳雍县| 额尔古纳市| 佛坪县| 崇文区| 南和县| 塔河县| 嫩江县| 六安市| 永新县| 莆田市| 开鲁县| 天镇县| 丘北县| 长沙市| 如皋市| 白河县| 韶关市| 芜湖县|