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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值