首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Python‘s exec Functions: Execute Dynamically Generated Code

Python‘s exec Functions: Execute Dynamically Generated Code

作者头像
geovindu
发布2026-06-18 20:35:06
发布2026-06-18 20:35:06
180
举报
代码语言:javascript
复制
# 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)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档