第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

全局變量不更新?

全局變量不更新?

狐的傳說 2021-03-18 18:19:15
import datetime as DTimport calendardef getMonthRanges(startDate, endDate):    while startDate <= endDate:        year, month = startDate.year, startDate.month        weekday, day = calendar.monthrange(year, month)        end = min(DT.date(year, month, day), endDate)        yield (startDate, end)        startDate = end+DT.timedelta(days=1)ranges = [map(str, interval)           for interval in getMonthRanges(DT.date(2011,9,11), DT.date(2013,4,24))]print(ranges)產(chǎn)量[['2011-09-11', '2011-09-30'], ['2011-10-01', '2011-10-31'], ['2011-11-01', '2011-11-30'], ['2011-12-01', '2011-12-31'], ['2012-01-01', '2012-01-31'], ['2012-02-01', '2012-02-29'], ['2012-03-01', '2012-03-31'], ['2012-04-01', '2012-04-30'], ['2012-05-01', '2012-05-31'], ['2012-06-01', '2012-06-30'], ['2012-07-01', '2012-07-31'], ['2012-08-01', '2012-08-31'], ['2012-09-01', '2012-09-30'], ['2012-10-01', '2012-10-31'], ['2012-11-01', '2012-11-30'], ['2012-12-01', '2012-12-31'], ['2013-01-01', '2013-01-31'], ['2013-02-01', '2013-02-28'], ['2013-03-01', '2013-03-31'], ['2013-04-01', '2013-04-24']]分享全局變量不會更新,但是我將其導入到正在更新的解釋器中。請幫我如何更新字典 dimport texttableclass Lc:    d={50:8,100:6,200:4,500:3,1000:2,2000:1}    he=['container_size_in_ltrs','qty']    def inventory(self,g):        d1=self.d        if d1!=g:            d1=g        self.d=g        l1=d1.keys()        l1.sort()        li=[]        ls=[]        for i in l1:            li=[]            li.append(i)            li.append(d1[i])            ls.append(li)        table=texttable.Texttable()        table.header(self.he)        table.add_rows(ls,header=False)        print table.draw()    def add_inv(self,k,v):        d=self.d        for i,j in d.items():            if k==i:                d[k]=d[k]+v        print d        return dz=Lc()g=z.add_inv(500,2)print z.d
查看完整描述

1 回答

?
弒天下

TA貢獻1818條經(jīng)驗 獲得超8個贊

您的全局變量正在更新。提取部分代碼:


class Lc:


    d={50:8,100:6,200:4,500:3,1000:2,2000:1}

    he=['container_size_in_ltrs','qty']


    def add_inv(self,k,v):

        d=self.d

        for i,j in d.items():

            if k==i:

                d[k]=d[k]+v

        print d

        return d


z=Lc()

x=Lc()


print "Add 2 to 500, expect 500:5"

g=z.add_inv(500,2)

print z.d


# Notice that x.add_inv also modifies z.d

print "Add 5 to 500, expect 500:7 because x initialized d again"

h=x.add_inv(500,2)

print z.d

我得到:


Add 2 to 500, expect 500:5

{100: 6, 200: 4, 2000: 1, 1000: 2, 50: 8, 500: 5}

{100: 6, 200: 4, 2000: 1, 1000: 2, 50: 8, 500: 5}

Add 5 to 500, expect 500:7 because x initialized d again

{100: 6, 200: 4, 2000: 1, 1000: 2, 50: 8, 500: 7}

{100: 6, 200: 4, 2000: 1, 1000: 2, 50: 8, 500: 7}

我只是希望您確實試圖以d這種卑鄙的方式宣布其為全球性。


查看完整回答
反對 回復 2021-03-23
  • 1 回答
  • 0 關注
  • 257 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號