首页 > 编程语言 >Python's exec Functions: Execute Dynamically Generated Code

Python's exec Functions: Execute Dynamically Generated Code

时间:2024-10-27 14:47:04浏览次数:9  
标签:Functions Code return exec self numbers print def

 

 

# 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)

  

标签:Functions,Code,return,exec,self,numbers,print,def
From: https://www.cnblogs.com/geovindu/p/18508373

相关文章

  • leetCode-双指针
     移动零classSolution{publicvoidmoveZeroes(int[]nums){intn=nums.length;intslow=0;intfast=0;while(fast<n){if(nums[fast]!=0){nums[slow++]=nums[fast++];......
  • Codeforces Round 982 div2 个人题解(A~D2)
    CodeforcesRound982div2个人题解(A~D2)Dashboard-CodeforcesRound982(Div.2)-Codeforces火车头#define_CRT_SECURE_NO_WARNINGS1#include<algorithm>#include<array>#include<bitset>#include<cmath>#include<cstdio>#in......
  • 【Atcoder训练记录】AtCoder Beginner Contest 377
    训练情况赛后反思D题差一点点吧?可能不去乐跑就能写出来了A题我们发现ABC是字典序单调递增的,字符串先排序再判断是否为ABC即可。#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;voidsolve(){ strings;cin>>s; sort(s.begin(),s.end()); i......
  • COMP3811 Computer GraphicsModule code
    SchoolofComputing:assessmentbriefModuletitleComputerGraphicsModulecodeCOMP3811AssignmenttitleCoursework1AssignmenttypeanddescriptionProgrammingassignment:GraphicsfundamentalsRationaleThecourseworkrevolvesaroundfundamentalgraphi......
  • leetcode每日一题:3181.执行操作可获得的最大总奖励 II
     题干:读本文前,请先弄懂上一篇中的内容,因为这是对上一篇内容的优化:3180.执行操作可获得的最大总奖励I明白上篇的,访问值的影响、复制、上下行之间的关系和算法后可继续看:上一篇中,我们用二维数组,第二维表示了状态空间。但是,在今日的题目中,提交不行,因为占用的空间太太太......
  • Codeforces Round 980 (Div. 2)
    目录写在前面A签到B贪心,模拟C贪心,结论,思维D图论转化,最短路写在最后写在前面比赛地址:https://codeforces.com/contest/2030。赛时被B硬控1h,后面两题一眼秒了一共写了20min呃呃。还好是小号。A签到讨论一下很容易算出来最优决策。///*By:Luckyblock*/#include......
  • Codeforces Round 981 (Div. 3) 10.24 (ABCDE)题解
    CodeforcesRound981(Div.3)2024.10.24题解A.SakurakoandKosuke题意:\(Sakurako\)与\(Kosuke\)正在玩游戏,一个点在原点\(x=0\)的起始位置。对于第\(i\)次操作,点会移动\(2\asti-1\)步。两人轮流操作,Sakurako先手,每次将点往负方向移动;Kosuke每次将点往正方向移动......
  • Codeforces Round 979 (Div. 2)
    目录写在前面A签到B构造C博弈D模拟E组合数学写在最后写在前面比赛地址:https://codeforces.com/contest/2030。赛时E看错题了变成神题了而且居然还口胡了一个自然根号的做法还写出来了然而样例没过最后才发现读错题了妈的。掉分!A签到\(b,c\)即前缀最小值和最大值,显......
  • LeetCode 3181. 执行操作可获得的最大总奖励 II
    1classSolution{2public:3intmaxTotalReward(vector<int>&rewardValues){4intm=ranges::max(rewardValues);5unordered_set<int>s;6for(intv:rewardValues){7if(s.contains(v))......
  • vscode远程连接linux服务器
    要在远程服务器上使用VisualStudioCode(VSCode)的图形界面,可以通过以下几种方法实现。最常见的方法是使用VSCode的RemoteDevelopment插件,这样你可以在本地机器上使用VSCode连接并编辑远程服务器上的文件。方法一:使用VSCodeRemote-SSH插件1.安装Remote......