小碼哥

          誰謂河廣,一葦杭之

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            7 隨筆 :: 17 文章 :: 74 評論 :: 0 Trackbacks

          常用鏈接

          留言簿(21)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          訂閱Canvas

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          以教程中的一段小詩開始:
          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()

          哇,下面這段真是解釋性的腳本語言的魅力啊,動態生成語句,然后直接解釋執行,太靈活了

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

          直接輸出了a的值10

          還有,更厲害的是可以給對象動態的添加屬性,不知道這么理解對不對

           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 小碼哥 閱讀(314) 評論(0)  編輯  收藏 所屬分類: python
          主站蜘蛛池模板: 台南市| 恩平市| 鱼台县| 大同市| 长乐市| 桃园市| 昭平县| 太仓市| 青海省| 闽清县| 泰安市| 保康县| 施甸县| 平利县| 白沙| 长治县| 苗栗市| 阳泉市| 克拉玛依市| 乳山市| 绥化市| 本溪| 稻城县| 连平县| 乌兰察布市| 仪陇县| 平度市| 呼玛县| 苍梧县| 辽宁省| 团风县| 玉田县| 民权县| 甘孜县| 兴安盟| 贵溪市| 甘洛县| 佛冈县| 天柱县| 雅江县| 北碚区|