dir(),divmod(),eval(),exec() 2018-10-6

#内置函数源码

def dir(p_object=None): # real signature unknown; restored from __doc__
    """
    dir([object]) -> list of strings
    
    If called without an argument, return the names in the current scope.
    Else, return an alphabetized list of names comprising (some of) the attributes
    of the given object, and of attributes reachable from it.
    If the object supplies a method named __dir__, it will be used; otherwise
    the default dir() logic is used and returns:
      for a module object: the module's attributes.
      for a class object:  its attributes, and recursively the attributes
        of its bases.
      for any other object: its attributes, its class's attributes, and
        recursively the attributes of its class's base classes.
    """
    return []

def divmod(x, y): # known case of builtins.divmod
    """ Return the tuple (x//y, x%y).  Invariant: div*y + mod == x. """
    return (0, 0)

def eval(*args, **kwargs): # real signature unknown
    """
    Evaluate the given source in the context of globals and locals.
    
    The source may be a string representing a Python expression
    or a code object as returned by compile().
    The globals must be a dictionary and locals can be any mapping,
    defaulting to the current globals and locals.
    If only globals is given, locals defaults to it.
    """
    pass

def exec(*args, **kwargs): # real signature unknown
    """
    Execute the given source in the context of globals and locals.
    
    The source may be a string representing one or more Python statements
    or a code object as returned by compile().
    The globals must be a dictionary and locals can be any mapping,
    defaulting to the current globals and locals.
    If only globals is given, locals defaults to it.
    """
    pass


'''
百度翻译:
		DEF DIR(PyObjult=无):未知的实签名;从

		“”

		DIR([Obj])->字符串列表



		如果没有参数调用,则返回当前范围中的名称。

		否则,返回字母名称的列表,包括(部分)属性。

		给定的对象和可从其获得的属性。

		如果对象提供了一个名为y.dIrthix的方法,则将使用该方法,否则

		默认的DR()逻辑被使用并返回:

		对于模块对象:模块的属性。

		用于类对象:属性及其递归属性

		它的基础。

		对于任何其他对象:它的属性,它的类的属性,以及

		递归地递归类的基类的属性。

		“”

		返回[]



		DEVMODD(x,y):已知的构建实例

		“返回元组(x//y,x%y)。不变量:div *y+mod==x。

		返回(0, 0)



		DEFEVE(*ARGs,**KWARGS):未知实签名

		“”

		在全球化和本地人的背景下评估给定的来源。



		源可以是表示Python表达式的字符串。

		或CaseRever()返回的代码对象。

		全局变量必须是字典,而局部变量可以是任何映射,

		默认为当前全局变量和本地变量。

		如果只给出全局变量,则局部变量默认为它。

		“”

		通过



		DEFEXEC(*ARGs,**KWARGS):未知实签名

		“”

		在全局和局部上下文中执行给定的源。



		源可以是表示一个或多个Python语句的字符串。

		或CaseRever()返回的代码对象。

		全局变量必须是字典,而局部变量可以是任何映射,

		默认为当前全局变量和本地变量。

		如果只给出全局变量,则局部变量默认为它。

		“”

		通过

'''

direct = '''
目录无链接:

1 scope
2 alpahabetized
3 comprising
4 reachable
5 otherwise
6 logic
7 recursively
8 invariant
9 mapping
10 evaluate

'''

context = '''

1 scope
		[ 英  [skəʊp]   美  [skop]
		n. 范围;余地;视野;眼界;导弹射程
		vt. 审视]
		{the range of things that a subject, activity, book etc deals with
		〔学科、活动、书籍等的〕范围}
		<return the names in the current scope:
		返回当前范围的所有名字(变量名)>






2 alphabetize <=> alphabetize
		[ 英  ['ælfəbətaɪz]   美  ['ælfəbə'taɪz]
			vt. 依字母顺序排列;用字母表示;拼音化]
		{to arrange(安排,排列) things in order according(
		 使一致) to the letters of the alphabet(字母表)
			按字母顺序排列}
		<return an alphabetized list of names comprising (
			some of) the attributes:
			返回按字母排序的变量名的列表,包括(部分)属性>





		
3 comprising <=> comprise
		[
			 英  [kəm'praɪz]   美  [kəm'praɪz]
			vt. 包含;由…组成
			]
		{If you say that something comprises or is comprised of a number of things or people, you mean it has them as its parts or members. 包含; 由…组成}
		<comprising (some of) the attributes
			包含(部分)属性>
		





4 reachable
		[ 英  ['riːtʃəbl]   美  ['ritʃəbl]
			adj. 可获得的;可达成的]
		{	softly reachable 软可及的
			Reachable Zone 可接触区域
			reachable object 可达对象
			Reachable Accounts 成功联系量
			reachable vertex 可达顶点
			reachable neighbor 可达邻域
			reachable domain 可达范围
			Stay Reachable 保持平易近人
			reachable subnet 可达子网}
		<and of attributes reachable from it:
			以及从中可获得的属性>





		
5 otherwise
		[英 [ˈʌðəwaɪz]   美 [ˈʌðərwaɪz]  
			adv.
			否则;另外;别的方式
			adj.
			别的,另外的;不同的
			conj.
			否则,不然]
		{used when saying what bad thing will happen if something is not done
			否则,要不然 〔指如果不做某事,就会发生不好的事〕}
		<otherwise the default dir() logic is 
    		used and returns:
    		否则,默认的dir()逻辑就会被使用并返回(如下值:)>





		
6 logic
		[ 英  ['lɒdʒɪk]   美  ['lɑdʒɪk]
			n. 逻辑;逻辑学;逻辑性
			adj. 逻辑的]
		{a way of thinking about something that seems correct and reasonable, or a set of sensible
		(明智的; 通情达理的; 合乎情理的; 意识到的,能感觉到的;) reasons for doing something
			逻辑;〔做某事的〕道理,合理的想法a set of 一套}
		<逻辑>
		





7 recursively
		[[ri'kəsivli] 
			adv. 递归地;递回地]
		{   recursively computable 递归可计算的
			recursively constructing 递归构造
			recursively projective 递归射影的
			recursively invariance 递归不变性
			Include Recursively 包括当前文件夹
					以及它的子文件夹内所有的文件夹
			recursively axiomatizable 递归可公理化
			recursively isomorphism 递归同构
			Recursively Representative 递归可表示性
			recursively presentable 递归可表现}
		<for a class object:  its attributes, and recursively the attributes
			用于类对象,(返回)它的属性和递归属性>

		




8 invariant
		[ 英  [ɪn'veərɪənt]   美  [ɪn'vɛrɪənt]
			adj. 不变的
			n. [数] 不变量;[计] 不变式]
		{ an entity(实体,存在,本质), quantity(量,数量,大量,总量ˋkwɑntətɪ /), etc,

		that is unaltered(不变的/ʌnˈɔːltəd/) by a 

		particular(特别的) transformation(转化) of 
		coordinates(坐标,相配之衣物,使协调 /kəʊˈɔːdɪnɪts, -ˌneɪts/) 不变式; 不变量
		百度:不受特定坐标变换影响的实体、数量等

				invariant mass 不变质量
				invariant mass 固有质量
				Casimir invariant 卡西米尔不变量
				Hopf invariant 霍普夫不变量
				time invariant 时间不变量
				invariant point 不变点
				invariant function 不变函数
				invariant subgroup 不变子群
				integral invariant 积分不变式
				integral invariant 积分不变量
				optical invariant 光学不变量
				optical invariant 光学间隔}
		<Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
		百度:返回元组(x//y,x%y)。不变量:div *y+mod==x?>
		
9 mapping <=> map
		[ 英  ['mæpɪŋ]   美  ['mæpɪŋ]
			v. 绘图;筹划(map的ing形式)
			n. 地图;绘图;[数] 映像]
		{	texture mapping 材质贴图
			texture mapping 纹理映射
			texture mapping 纹理贴图
			texture mapping 贴图坐标
			space mapping 空间映射
			space mapping 空间制图
			space mapping 空间映射法
			Squeeze mapping 挤压映射
			Cylindrical Mapping 圆柱体贴图
			Cylindrical Mapping 圆柱体映射
			Cylindrical Mapping 圆柱贴图
			Cylindrical Mapping 圆柱映射
			concept mapping 概念构图
			concept mapping 概念图
			concept mapping 概念映射
			concept mapping 概念绘图
			inverse mapping 逆映射
			inverse mapping 反向映射
			inverse mapping 逆映像
			inverse mapping 材质处理逆向算法
			Delete Mapping 删除贴图
			Automatic Mapping 自动映射
			Automatic Mapping 自动绘图
			Automatic Mapping 自动贴图
			Automatic Mapping 卷展栏
			aerial mapping 航空测绘
			aerial mapping 航空测图
			aerial mapping 航空写像
			aerial mapping 航摄制图}
		<The globals must be a dictionary and locals can be any mapping:
			全局变量必须是字典,局部变量可以是任何映射。

			eval(expression[, globals[, locals]])
			expression -- 表达式。
			globals -- 变量作用域,全局命名空间,如果被提供,
			则必须是一个字典对象。
			locals -- 变量作用域,局部命名空间,如果被提供,
			可以是任何映射对象。>
		



10 evaluate 
			[ 英  [ɪ'væljʊeɪt]   美  [ɪ'væljʊ'et]
				vt. 评价;估价;求…的值
				vi. 评价;估价]
			{to judge how good, useful, or successful
				 something is 评估,评价}
			<Evaluate the given source in the context of globals and locals
			  在全局和局部的上下文中评估给定的源(码)>





函数dir()
			dir() 函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。

			语法

			dir 语法:
			dir([object])

			参数说明:

			object -- 对象、变量、类型。

			---------------------

			本段内容摘自 苏源流 的CSDN 博客




函数divmod()
			把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)。例如10/3 = 3……1 (3余1)
			divmod(10,3) ---> (3,1)

				>>> divmod(10,3)
				(3, 1)
				>>> 


函数eval()和exec(),可以将引号里的命令执行,input()的数据类型为str
				  eval(input()) , exec(input())就可以直接解读
				  input的内容
		运用场景:
				比如你做了一个记账程序,要修改信用卡的可用额度
				amount_usable = input('请输入现在的可用额度')
				eval(amout_usable),可用额度就由字符串类型变成了数字类型

exec()与eval()的区别:
	exec()可以解悉带=号的表达式,eval()不行。看单词意思就可以知道谁的功能
	eval<=>evaluate 求……值。
	exec<=>excute 执行


具体参考博客:https://www.cnblogs.com/yangmingxianshen/p/7810496.html





'''



>>> data = {'1':1 , '2':2}
>>> text = 'data['1'] = 111'
  File "<stdin>", line 1
    text = 'data['1'] = 111'
                  ^
SyntaxError: invalid syntax #外面和里面的引号要区分开。
>>> text = "data['1'] = 111"
>>> text
"data['1'] = 111"
>>> eval(text)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    data['1'] = 111
              ^
SyntaxError: invalid syntax #说=是不支持的语法
>>> exec(text)
>>> data
{'1': 111, '2': 2}#被exec改成功了
>>> 

exec()与eval()与compile()具体区别参考博客:https://www.cnblogs.com/yangmingxianshen/p/7810496.html

dir()具体:https://blog.csdn.net/kyjl888/article/details/78940805?utm_source=copy

posted @ 2018-10-06 13:08  Charles_Wu  阅读(305)  评论(0编辑  收藏  举报