楼主: Lisrelchen
1613 3

Advanced Python 3 Programming Techniques [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2015-7-12 09:18:55 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  • Advanced Python 3 Programming Techniques
  • By: Mark Summerfield

  • Publisher: Addison-Wesley Professional

  • Pub. Date: February 13, 2009

  • Print ISBN-10: 0-321-63551-5

  • Print ISBN-13: 978-0-321-63551-8

  • Web ISBN-10: 0-321-63772-0

  • Web ISBN-13: 978-0-321-63772-7

  • Pages in Print Edition: 68

  • Subscriber Rating: [1 Rating]




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Programming Techniques Technique Advanced ADVANCE

本帖被以下文库推荐

沙发
Lisrelchen 发表于 2015-7-12 09:20:59
4.1. Dynamic Code Execution

The easiest way to execute an expression is to use the built-in eval() function. For example:

x = eval("(2 ** 31) - 1")    # x == 2147483647

This is fine for user-entered expressions, but what if we need to create a function dynamically? For that we can use the built-in exec() function. For example, the user might give us a formula such as 4πr2 and the name “area of sphere”, which they want turned into a function. Assuming that we replace π with math.pi, the function they want can be created like this:

  1. import math
  2. code = '''
  3. def area_of_sphere(r):
  4.     return 4 * math.pi * r ** 2
  5. '''
  6. context = {}
  7. context["math"] = math
  8. exec(code, context)
复制代码

We must use proper indentation—after all, the quoted code is standard Python. (Although in this case we could have written it all on a single line because the suite is just one line.)

If exec() is called with some code as its only argument there is no way to access any functions or variables that are created as a result of the code being executed. Furthermore, exec()cannot access any imported modules or any of the variables, functions, or other objects that are in scope at the point of the call. Both of these problems can be solved by passing a dictionary as the second argument. The dictionary provides a place where object references can be kept for accessing after the exec() call has finished. For example, the use of thecontext dictionary means that after the exec() call, the dictionary has an object reference to the area_of_sphere() function that was created by exec(). In this example we neededexec() to be able to access the math module, so we inserted an item into the context dictionary whose key is the module’s name and whose value is an object reference to the corresponding module object. This ensures that inside the exec() call, math.pi is accessible.

In some cases it is convenient to provide the entire global context to exec(). This can be done by passing the dictionary returned by the globals() function. One disadvantage of this approach is that any objects created in the exec() call would be added to the global dictionary. A solution is to copy the global context into a dictionary, for example, context = globals().copy(). This still gives exec() access to imported modules and the variables and other objects that are in scope, and because we have copied, any changes to the context made inside the exec() call are kept in the context dictionary and are not propagated to the global environment. (It would appear to be more secure to use copy.deepcopy(), but if security is a concern it is best to avoid exec() altogether.) We can also pass the local context, for example, by passing locals() as a third argument—this makes objects in the local scope accessible to the code executed by exec().

After the exec() call the context dictionary contains a key called "area_of_ sphere" whose value is the area_of_sphere() function. Here is how we can access and call the function:

  1. area_of_sphere = context["area_of_sphere"]
  2. area = area_of_sphere(5)        # area == 314.15926535897933
复制代码

The area_of_sphere object is an object reference to the function we have dynamically created and can be used just like any other function. And although we created only a single function in the exec() call, unlike eval(), which can operate on only a single expression, exec() can handle as many Python statements as we like, including entire modules, as we will see in the next subsubsection.


藤椅
bailihongchen 发表于 2015-7-13 17:14:02
thanksf orsaring

板凳
franky_sas 发表于 2016-12-16 14:40:36

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-4 12:41