
Python
文章平均质量分 74
iCode0410
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
python 函数嵌套
参考资料 在Python中函数可以作为参数进行传递,而也可以赋值给其他变量(类似Javascript,或者C/C++中的函数指针); 类似Javascript,Python支持函数嵌套,Javascript嵌套函数的应用模式对Python适用; >>> def multiplier(factor): ... def multiple(numb原创 2014-09-18 17:23:19 · 1069 阅读 · 0 评论 -
python __setattr__, __getattr__, __delattr__,__getattribute__
参考资料 __setattr__、__getattr__和__delattr__以及__getattribute__可以拦截对对象属性的访问; >>> s = Something() >>> s.age = 3 set 'age' = 3 >>> s.age 3 注意到,s.age并没有调用__getattr__,是因为原创 2014-09-20 08:59:14 · 2094 阅读 · 0 评论 -
Python,异常 exception
同Java一样,Pyton异常对象来表示异常情况。遇到错误后,引发异常,如果异常对象并未对处理或捕捉,程序就会用所谓的回溯(Traceback)来终止执行; >>> 1/0 Traceback (most recent call last): File "", line 1, in ZeroDivisionError: division by zero 程序可以通过rais原创 2014-09-19 17:25:44 · 3957 阅读 · 0 评论 -
Python 类继承,__bases__, __mro__, super
Python是面向对象的编程语言,也支持类继承。 >>> class Base: ... pass ... >>> class Derived(Base): ... pass 这样就定义了两个类,Derived继承了Base。issubclass(a,b)可以测试继承关系: >>> issubclass(Derived, Base) True 在原创 2014-09-19 16:42:34 · 8873 阅读 · 1 评论 -
python - Local variable referenced before assignment
直接上代码 def foo(): a = 1 def bar(): a = a + 1 return bar() 运行foo(),报错 import dis def foo(): a = 1 def bar(): a = a + 1 dis.dis(bar) return bar() dis打印的内容如下: 很容易明白其意原创 2013-10-26 21:52:31 · 4140 阅读 · 0 评论 -
Python property,属性
参考资料 http://www.ibm.com/developerworks/library/os-pythondescriptors/ 顾名思义,property用于生成一个属性,通过操作这个属性,可以映射为对某些函数的操作,类似于C#。 形式为 pvar = propery(get_func, set_func, del_fun, doc_func)原创 2014-09-19 19:31:44 · 3937 阅读 · 0 评论 -
Python类,特殊方法, __getitem__,__len__, __delitem__
参考资料 特殊函数一般以__methodname__的形式命名,如:__init__(构造方法), __getitem__、 __setitem__(subscriptable所需method), __delitem__(del obj[key]所需method), __len__(len(…)所需method)等; 以下以什么都不做的Something原创 2014-09-19 18:29:25 · 3968 阅读 · 0 评论 -
python 静态方法,类成员方法
参考资料 静态方法和类成员方法分别在创建时被装入StaticMethod类型和Classmethod类型的对象中;静态方法的定义没有self参数,且能够被类本身直接调用;类方法在定义时需要名为cls的类似于self的参数,类成员方法可以被类的实例调用,而cls参数是自动被绑定到类的。 简单示例: >>> Something.smethod()原创 2014-09-19 19:51:43 · 1059 阅读 · 0 评论 -
Python类
Python使用中面向对象的语言,支持继承、多态; 定义一个Person类: >>> class Person: ... def sayHello(self): ... print('hello') ... >>> Person.sayHello(None) hello >>> Person().sayHello() hello 可以修原创 2014-09-18 20:59:00 · 1157 阅读 · 0 评论 -
Python 函数
定义一个什么都不做的函数 >>> def a(): ... pass ... >>> def printHello(): ... print("hello") ... >>> printHello() hello >>> callable(printHello) True 顾名思义,callable函数用于判断函数是否可以调用原创 2014-09-18 16:47:00 · 663 阅读 · 0 评论 -
Python exec, eval
参考资料 通过exec可以执行动态Python代码,类似Javascript的eval功能;而Python中的eval函数可以计算Python表达式,并返回结果(exec不返回结果,print(eval("…"))打印None); >>> exec("print(\"hello, world\")") hello, world >>> a =原创 2014-09-18 15:52:56 · 895 阅读 · 0 评论 -
python module, package
任何Python程序都可以作为模块导入;在导入自己的模块时,需要添加路径: import sys sys.path.append('absolute-path'); (__pycache__是执行main.py时创建的) hello.py内容: def sayHello(): print('hello,world') main.py原创 2014-09-20 16:56:53 · 1262 阅读 · 0 评论