python如何?類似php的引用賦值

來源:互聯網
上載者:User

標籤:ati   %s   oba   輸出變數   bsp   存在   index   div   color   

直接放代碼,有注釋,就不解析了

 1 # ############################ 2 # 改變一個變數的值,與之有關係的變數的值也會相等變化 3 # 類似php的引用賦值,宏觀來看 4 # ############################ 5  6 relation = []   # relation是裝在有關係的集合的列表 7  8  9 # 綁定有關係的變數10 def bind(*arg):11     if len(arg) < 2:12         ‘‘‘13         判斷綁定變數的個數14         ‘‘‘15         print("error: 至少綁定兩個變數")16         exit()17     global relation18     for i in arg:19         ‘‘‘20         檢查綁定的變數是否存在21         ‘‘‘22         if i not in globals().keys():23             print("error: 變數%s不存在"%(i))24             exit()25 26     # 進行裝載27     flag = set(arg)28     if len(relation):29         i = 030         while i < len(relation):31             if len(relation[i] & flag):32                 relation[i] = relation[i] | flag33                 break34             i += 135         if i == len(relation):36             relation.append(flag)37     else:38         relation.append(flag)39 40     # 參數中,其餘的變數的值等於以第一個變數參數的值41     index = 142     while index < len(arg):43         globals()[arg[index]] = globals()[arg[0]]44         index += 145 46 47 # 給變數賦值48 def assign(var, value):49     global relation50     if len(relation):51         i = 052         while i < len(relation):53             if var in relation[i]:54                 for item in relation[i]:55                     globals()[item] = value56                 break57             i += 158         if i == len(relation):59             print(‘error: 賦值的變數尚未綁定‘)60             exit()61     else:62         print("error: 當前不存在有關係的集合")63         exit()64 65 66 a = ‘sky‘67 b = ‘land‘68 bind(‘a‘, ‘b‘)  # 綁定有關係變數69 print(relation)     # 列印當前有關係集合的列表 [{‘a‘, ‘b‘}]70 print(a, b)     # 輸出變數a和b的值 sky sky71 assign(‘a‘, ‘sea‘)  # 改變變數的值72 print(a, b)     # 輸出變數a和b的值 sea sea

 

python如何?類似php的引用賦值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.