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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在python中的類函數(shù)中更改全局變量

在python中的類函數(shù)中更改全局變量

慕碼人8056858 2021-07-02 06:00:35
我之前看過有關(guān)此問題的問題,但我無法在類函數(shù)中重新創(chuàng)建全局變量的更改:test = 0class Testing:    def add_one():        global test        test += 1當(dāng)我輸入時(shí)Testing.add_oneprint (test)它打印“0”。如何獲取類中的函數(shù)以添加一個(gè)進(jìn)行測(cè)試?
查看完整描述

3 回答

?
森林海

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊

你沒有調(diào)用函數(shù)。如果你這樣做了,你會(huì)得到一個(gè) TypeError


應(yīng)該是這樣的


test = 0

class Testing(object):

    @staticmethod

    def add_one():

        global test

        test += 1


Testing.add_one()


查看完整回答
反對(duì) 回復(fù) 2021-07-13
?
白衣染霜花

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊

您應(yīng)該調(diào)用該方法。那么只有它會(huì)增加變量的值test。


In [7]: test = 0

   ...: class Testing:

   ...:     def add_one():

   ...:         global test

   ...:         test += 1                                                                                                                                                                


# check value before calling the method `add_one`

In [8]: test

Out[8]: 0


# this does nothing

In [9]: Testing.add_one

Out[9]: <function __main__.Testing.add_one()>


# `test` still holds the value 0

In [10]: test

Out[10]: 0


# correct way to increment the value

In [11]: Testing.add_one()


# now, check the value

In [12]: test

Out[12]: 1


查看完整回答
反對(duì) 回復(fù) 2021-07-13
?
吃雞游戲

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

試試這個(gè),

http://img1.sycdn.imooc.com//60ed7aec00016b7603570095.jpg

test = 0

class Testing:

    def add_one(self):

        global test

        test += 1

        print(test)


t = Testing()

t.add_one()


查看完整回答
反對(duì) 回復(fù) 2021-07-13
  • 3 回答
  • 0 關(guān)注
  • 508 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)