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

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

Python - 覆蓋父類屬性而不實(shí)例化

Python - 覆蓋父類屬性而不實(shí)例化

不負(fù)相思意 2023-09-12 16:52:14
如何在不使用任一類的對(duì)象實(shí)例的情況下覆蓋子類中的父類屬性?來(lái)自 Java/C++ 及其嚴(yán)格結(jié)構(gòu)設(shè)計(jì)的世界,我發(fā)現(xiàn)自己受到 Python 做事方式的挑戰(zhàn)。我想保持相對(duì)靜止。例子:from urllib.parse import urljoinclass base:    host = "/host/"    path = "Override this in child classes"    url = urljoin(host, path)class config(base):    path = "config"    @classmethod    def print_url(cls):        print(cls.url) # Currently prints "/host/Override this in child classes"                       # Would like to print "/host/config" insteadclass log(base):    path = "log"    @classmethod    def print_url(cls):        print(cls.url) # Currently prints "/host/Override this in child classes"                       # Would like to print "/host/log" instead所需用途:>>> config.print_url()/host/config>>> log.print_url()/host/log我希望config.path和log.path屬性能夠覆蓋base.path. 這樣我就可以u(píng)rl = urljoin(host, path)在類中一勞永逸地使用base(并且避免必須在每個(gè)派生類中復(fù)制/粘貼相同的屬性/計(jì)算)。我無(wú)法弄清楚如何在不構(gòu)造對(duì)象的情況下完成此任務(wù)(我希望避免)。有人有什么建議嗎?提前致謝!
查看完整描述

1 回答

?
守著一只汪

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

子path屬性會(huì)覆蓋base.path。您不覆蓋的是url屬性。當(dāng)運(yùn)行 的主體base來(lái)創(chuàng)建類對(duì)象時(shí),就會(huì)計(jì)算一次。


未來(lái)你有幾個(gè)選擇。無(wú)論哪種方式,您都需要url動(dòng)態(tài)地進(jìn)行計(jì)算,或者每次訪問(wèn)它時(shí),或者每個(gè)子類至少一次。


最簡(jiǎn)單的方法是制作url成classmethod:


class base:

    host = "/host/"

    path = "Override this in child classes"


    @classmethod

    def url(cls):

        return urljoin(cls.host, cls.path)


    @classmethod

    def print_url(cls):

        print(cls.url())


class config(base):

    path = "config"


class log(base):

    path = "log"

請(qǐng)注意,您現(xiàn)在指的是實(shí)際的類host并且path是動(dòng)態(tài)的。您還只需要一種print_url方法,base而不是每個(gè)類中使用不同的方法。


base另一種選擇是為 及其所有子級(jí)提供一個(gè)元類,url其形式為property:


class url_meta(type):

    @property

    def url(cls):

        return urljoin(cls.host, cls.path)


class base(metaclass=url_meta):

    host = "/host/"

    path = "Override this in child classes"


    @classmethod

    def print_url(cls):

        print(cls.url)


class config(base):

    path = "config"


class log(base):

    path = "log"

這是可行的,因?yàn)?python 類也是對(duì)象。property您可以在類的類(元類)中定義 a ,它的行為與任何property對(duì)實(shí)例的行為一樣。這次實(shí)例本身就是一個(gè)類。


第三種選擇是確保url在每個(gè)子項(xiàng)中靜態(tài)但正確地定義它。該__init_subclass__方法允許您直接從以下位置非常方便地執(zhí)行此操作base:


class base:

    host = "/host/"

    path = "Override this in child classes"

    url = urljoin(host, path)


    @classmethod

    def __init_subclass__(cls):

        cls.url = urljoin(cls.host, cls.path)


    @classmethod

    def print_url(cls):

        print(cls.url)


class config(base):

    path = "config"


class log(base):

    path = "log"

您也可以使用元類完成同樣的事情:


class url_meta2(type):

    def __init__(cls, *args, **kwargs):

        cls.url = urljoin(cls.host, cls.path)


class base(metaclass=url_meta2):

    host = "/host/"

    path = "Override this in child classes"


    @classmethod

    def print_url(cls):

        print(cls.url)


class config(base):

    path = "config"


class log(base):

    path = "log"


查看完整回答
反對(duì) 回復(fù) 2023-09-12
  • 1 回答
  • 0 關(guān)注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報(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)