Python's exec Functions: Execute Dynamically Generated Code

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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)

  

posted @   ®Geovin Du Dream Park™  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-10-27 Python: Memento Pattern
2022-10-27 Python: Command Pattern
2016-10-27 Concurrency in csharp (Asynchronous, Parallel, and Multithreaded Programming)
2015-10-27 csharp: InvokeHelper
2015-10-27 Free Slideshow, Gallery And Lightboxes Scripts
2015-10-27 35 Gallery – Ajax Slide
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示