Python's exec Functions: Execute Dynamically Generated Code
# encoding: utf-8 # 版權所有 2024 ©塗聚文有限公司 # 許可資訊查看:言語成了邀功的功臣,還需要行爲每日來值班嗎? # 描述: 主、子表單 窗體傳值 Parent-child form operations # Author : geovindu,Geovin Du 塗聚文. # IDE : PyCharm 2023.1 python 3.11 # OS : windows 10 # Datetime : 2024/10/27 13:09 # User : geovindu # Product : PyCharm # Project : IctGame # File : ui/main.py # explain : 學習 # Python's exec Functions: Execute Dynamically Generated Code import sys import os import io import re from math import * import asyncio class AsyncMange(object): """ """ def __init__(self): self.__xNmae=None @property def xName(self): """ :return: """ return self.__xNmae @xName.setter def xNmae(self,xname): """ :param xname: :return: """ self.__xNmae=xname async def Add(self): """ :return: """ print('add',self.xName) pass async def Edit(self): """ :return: """ self.__xNmae=44 print('edit',self.xName) pass async def __aenter__(self): """ :return: """ return self async def __aexit__(self,ty,val,tb): """ :param exc_type: :param exc_val: :param exc_tb: :return: """ pass async def main(): """ :return: """ async with AsyncMange() as m: m.xNmae=23 await m.Add() await m.Edit() asyncio.run(main()) numbers = [2, 3, 7, 4, 8] exec("result = sum( number**2 for number in numbers if number % 2 == 0)") print(result) exec("print(dir())", {}) exec('print(fact(5))', {'fact': factorial}) exec("name = input('Your name: ')\nprint(f'Hello, {name}!')") stringInput = """ def sumSquares(numbers): return sum(number**2 for number in numbers if number % 2 == 0) print(sumSquares(numbers)) """ compiled_code = compile(stringInput, "<string>", "exec") exec(compiled_code) numbers = [12, 23, 37, 44, 58] exec(compiled_code) sumSquares(numbers) print(sumSquares(numbers)) with open("geovindu.py", mode="r", encoding="utf-8") as hello: code = hello.read() exec(code)
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)