上一页 1 ··· 22 23 24 25 26
摘要: 简单总结几个常用的__code__的用法: (1)func.__code__.co_argcount:返回函数的参数个数,这里的参数个数不包含*args与**kwargs,具体来讲就是*args前的参数。 >>> def a(a,b,c,d=2,e=3,*arg,f,g=8,**kwargs): p 阅读全文
posted @ 2019-03-14 21:12 JohnYang819 阅读(5880) 评论(0) 推荐(1) 编辑
摘要: 装饰器的作用是在已有的可调对象(callable object)的基础上,插入代码,以增强或者管理可调对象,装饰器的核心就是通过传入一个可调对象,然后返回一个可调对象,就其装饰的对象而言,可以分为函数装饰器和类装饰器,就其构造方法而言,可以用嵌套函数(nested functions)或者类方法。 阅读全文
posted @ 2019-03-08 11:37 JohnYang819 阅读(234) 评论(0) 推荐(0) 编辑
摘要: (1)sys.argv是什么 sys模块为进入解释器维护或使用的变量,以及与解释器相关的函数提供了途径。sys.argv在脚本程序中扮演了这样一个角色:将命令行输入的参数作为一个list传入脚本程序,即我们可以在cmd命令形式驱动程序方法下,将设定的参数传入脚本,argv[0]是驱动脚本的名字(由操 阅读全文
posted @ 2019-03-05 21:30 JohnYang819 阅读(605) 评论(0) 推荐(0) 编辑
摘要: __getattr__与__getattribute__均是一般实例属性截取函数(generic instance attribute interception method),其中,__getattr__可以用在python的所有版本中,而__getattribute__只可以用到新类型类中(Ne 阅读全文
posted @ 2019-03-03 10:27 JohnYang819 阅读(1076) 评论(0) 推荐(0) 编辑
摘要: __slots__在python中是扮演属性声明(Attribute Declaration)的角色,当然属性本身不用声明,只需要在使用前赋值即可,但是实际上,属性的应用远非在使用前赋值就行,所以有attribute declaration, attribute accessor,attribute 阅读全文
posted @ 2019-03-02 21:47 JohnYang819 阅读(1120) 评论(0) 推荐(1) 编辑
摘要: python2.x中的新类型类(New-style class)与python3.x的类一致,均继承object类,而不继承object的类称为经典类(classic class),而对于这两种类,一般实例属性截取函数(generic instance attribute interception 阅读全文
posted @ 2019-03-02 17:09 JohnYang819 阅读(444) 评论(0) 推荐(0) 编辑
摘要: 在Python中,异常也可以嵌套,当内层代码出现异常时,指定异常类型与实际类型不符时,则向外传,如果与外面的指定类型符合,则异常被处理,直至最外层,运用默认处理方法进行处理,即停止程序,并抛出异常信息。如下代码: try: try: raise IndexError except TypeError 阅读全文
posted @ 2019-02-20 20:53 JohnYang819 阅读(9196) 评论(0) 推荐(0) 编辑
摘要: 众所周知,在python中,变量不需要事先声明,赋值后,即可调用使用。而调用的法则遵从LEGB法则,其中L为local,E为enclosing,G为Global,B为built-in,即变量首先在局部搜索,比如一个函数中,如果没有搜寻到,则继续在enclosing中搜寻,比如嵌套函数中更靠外侧的函数 阅读全文
posted @ 2019-02-18 20:09 JohnYang819 阅读(526) 评论(0) 推荐(0) 编辑
摘要: 1 """ 2 This script illustrate the usage of escape symbol %%. In general, it is used in mutil-formatted string. when a string is 3 formatted , %% can 阅读全文
posted @ 2019-02-07 14:39 JohnYang819 阅读(157) 评论(0) 推荐(0) 编辑
摘要: (1)背景 python中,对于类(自定义类)的实例对象的默认显示既没有太大用处,也不美观。比如: 1 class adder: 2 def __init__(self,value=0): 3 self.data=value #初始化数据 4 def __add__(self,other): 5 s 阅读全文
posted @ 2019-02-07 14:27 JohnYang819 阅读(418) 评论(0) 推荐(0) 编辑
上一页 1 ··· 22 23 24 25 26