欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?動態(tài)創(chuàng)建一個類的示例代碼

 更新時間:2025年06月19日 08:47:52   作者:JHC000000  
這篇文章主要介紹了Python?動態(tài)創(chuàng)建一個類的示例代碼,通過實例代碼補充介紹了python動態(tài)實例化的相關知識,需要的朋友可以參考下

Python 動態(tài)創(chuàng)建一個類的示例代碼:

# 動態(tài)創(chuàng)建類
# class_body 里是類里邊的函數(shù),可以寫在文件里,從文件中讀取
class_body = """
def running_function(self):
    print(self.name)
    print("running function")
def set_name(self,name):
    self.name = name
"""
class_dict = {}
# 加載類方法到class_dict里
exec(class_body,globals(),class_dict)
# 把class_dict中的方法加載到類里邊
Customer = type("Customer",(object,),class_dict)
# 實例化類,調用類方法
c = Customer()
c.set_name("12")
c.running_function()

補充:python動態(tài)實例化

python動態(tài)實例化

前言

最近在查一個服務的問題時,看到有一段代碼if .. elif ... 寫了近百行,類似

if command == "xxx":
	obj = CommandX()
	obj.run()
	# ...
elif command == "yyy":
	obj = CommandY()
	obj.run()
    # ...
elif command == "zzz":
	obj = CommandZ()
	obj.run()
    # ...
# ...

翻了下git記錄,最開始其實只有兩三個條件判斷,后來command越加越多,就這么延續(xù)下來了。

代碼邏輯其實沒什么問題,也很簡單明了,就是看起來有點丑,而且我還開了比較高的桌面縮放,導致一屏幕幾乎都是這段if ... elif

看來看去越發(fā)覺得丑,先寫個demo看看能不能跑通代碼。

方式1, 字典映射

如果需要判斷的條件比較少,用字典做映射還是挺方便的,但如果條件多,看起來還是挺丑的。

from abc import ABC, abstractmethod
class AbstractCommand(ABC):
    @abstractmethod
    def run(self):
        pass
class CommandA(AbstractCommand):
    def run(self):
        return "this is command A"
class CommandB(AbstractCommand):
    def run(self):
        return "this is command B"
class CommandC(AbstractCommand):
    def run(self):
        return "this is command C"
class CommandFactory:
    @staticmethod
    def create_command(command_type: str) -> AbstractCommand:
        command_mapping = {
            "cmda": CommandA,
            "cmdb": CommandB,
            "cmdc": CommandC
        }
        cls = command_mapping.get(command_type.lower())
        if not cls:
            raise ValueError(f"Unknown command type: {command_type}")
        return cls()
if __name__ == "__main__":
    cmd = CommandFactory.create_command("cmda")
    assert cmd.run() == "this is command A"
    cmd = CommandFactory.create_command("cmdb")
    assert cmd.run() == "this is command B"
    cmd = CommandFactory.create_command("cmdc")
    assert cmd.run() == "this is command CD"  # should be exception
    cmd = CommandFactory.create_command("cmdd")  # should be exception
    assert cmd.run() == "this is command D"

方式2, __init_subclass__

《流暢的Python(第2版)》的最后一章提到了這個__init__subclass__,根據python官方文檔:

當所在類派生子類時此方法就會被調用。cls 將指向新的子類。如果定義為一個普通實例方法,此方法將被隱式地轉換為類方法。傳給一個新類的關鍵字參數(shù)會被傳給上級類的 __init_subclass__。 為了與其他使用 __init_subclass__ 的類兼容,應當去掉需要的關鍵字參數(shù)再將其他參數(shù)傳給基類。

借助這個機制,可以在實現(xiàn)抽象基類時自動注冊子類,避免手動維護注冊表。

from abc import ABCMeta, abstractmethod
from threading import Lock
from collections import UserDict
class ThreadSafeDict(UserDict):
    """線程安全的字典"""
    def __init__(self):
        super().__init__()
        self._lock = Lock()
    def __setitem__(self, key, item):
        with self._lock:
            super().__setitem__(key, item)
class Command(metaclass=ABCMeta):
    registry = ThreadSafeDict()
    def __init__(self):
        pass
    @abstractmethod
    def run(self):
        pass
    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        cls.registry[cls.__name__.lower()] = cls  # 自動注冊子類
# 子類定義即自動注冊
class CommandA(Command):
    def run(self):
        return "this is command a!"
class CommandB(Command):
    def run(self):
        return "this is command b!"
class CommandC(Command):
    def run(self):
        return "this is command b!"
def create_command(command_type: str) -> Command:
    """工廠函數(shù)"""
    cls = Command.registry.get(command_type.lower())
    if not cls:
        raise ValueError(f"Unknown command type: {command_type}")
    return cls()
if __name__ == "__main__":
    cmd = create_command("CommandA")
    assert cmd.run() == "this is command a!"
    cmd = create_command("CommandB")
    assert cmd.run() == "this is command b!"
    cmd = create_command("CommandC")
    assert cmd.run() == "this is command cc!"  # should be exception
    cmd = create_command("CommandD")
    assert cmd.run() == "this is command b!"  # should be exception

乍一看還是挺不錯的,但是也有個缺點,那就是如果各個類分散在不同模塊中,那么工廠函數(shù)所在的模塊就要寫一堆from xxx import ...

如果module和類命名比較規(guī)范,也可以這么動態(tài)加載類

import importlib
def create_class(module_name, class_name):
    module = importlib.import_module(module_name)
    cls = getattr(module, class_name)
    return cls()

補充

自動注冊類看起來炫,但是對代碼閱讀來說不是很直觀。易讀還是美觀?這是一個問題。

到此這篇關于Python 動態(tài)創(chuàng)建一個類的文章就介紹到這了,更多相關Python 動態(tài)創(chuàng)建一個類內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python-pymysql如何實現(xiàn)更新mysql表中任意字段數(shù)據

    python-pymysql如何實現(xiàn)更新mysql表中任意字段數(shù)據

    這篇文章主要介紹了python-pymysql如何實現(xiàn)更新mysql表中任意字段數(shù)據問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Python使用窮舉法求兩個數(shù)的最大公約數(shù)問題

    Python使用窮舉法求兩個數(shù)的最大公約數(shù)問題

    這篇文章主要介紹了Python使用窮舉法求兩個數(shù)的最大公約數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • django最快程序開發(fā)流程詳解

    django最快程序開發(fā)流程詳解

    這篇文章主要介紹了django最快程序開發(fā)流程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • 基于Python實現(xiàn)加強版煙花

    基于Python實現(xiàn)加強版煙花

    這篇文章主要為大家詳細介紹了如何利用Python制作一個加強版煙花景,文中的示例代碼講解詳細,對我們學習Python有一定幫助,需要的可以參考一下
    2022-02-02
  • pytest自動化測試中的fixture的聲明和調用

    pytest自動化測試中的fixture的聲明和調用

    這篇文章主要為大家介紹了pytest自動化測試中的fixture的聲明和調用,文中含有詳細示例操作有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • Python中循環(huán)依賴問題及其解決方案

    Python中循環(huán)依賴問題及其解決方案

    在軟件開發(fā)中,循環(huán)依賴是一個常見的問題,尤其是在使用 Python 這樣的動態(tài)語言時,循環(huán)依賴指的是兩個或多個模塊或組件相互依賴,形成一個閉環(huán),本文將探討 Python 中循環(huán)依賴的問題,并提供一些解決方案,需要的朋友可以參考下
    2024-06-06
  • 人生苦短我用python python如何快速入門?

    人生苦短我用python python如何快速入門?

    這篇文章主要教大家如何快速入門python,一個簡短而全面的入門教程帶你走入Python的大門,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • python調用自定義函數(shù)的實例操作

    python調用自定義函數(shù)的實例操作

    在本文里我們給大家整理了關于python調用自定義函數(shù)的實例操作相關內容,有此需要的朋友們可以學習參考下。
    2019-06-06
  • Python3實現(xiàn)的簡單三級菜單功能示例

    Python3實現(xiàn)的簡單三級菜單功能示例

    這篇文章主要介紹了Python3實現(xiàn)的簡單三級菜單功能,涉及Python用戶交互以及針對json格式數(shù)據的遍歷、讀取、判斷等相關操作技巧,需要的朋友可以參考下
    2019-03-03
  • Python網絡爬蟲之Web網頁基礎

    Python網絡爬蟲之Web網頁基礎

    我們在學習爬蟲之前,要先了解網頁的組成,只有我們了解其組成嗎,才可以方能百戰(zhàn)百勝,文章中有詳細的代碼示例,需要的朋友可以參考一下
    2023-04-04

最新評論