Python 入门(二)
来源:互联网 发布:python 程序运行时间 编辑:程序博客网 时间:2024/05/21 19:23
1、函数的使用
# 语法:函数以 def 为标记, def a(b): b = 2 return b# 可以用 *params 传递多个实参In [94]: def a(b): ...: b = 2 ...: return b ...: In [95]: a(c)Out[95]: 2In [96]: cOut[96]: 3In [97]: def b(*c): ...: for i in c: ...: print(i) ...: In [98]: b(2)2In [99]: b(23)23In [100]: b([2, 34, 56])[2, 34, 56]In [101]: b(2, 3, 4, 5, 7)23457# 实参若是列表, 则在函数中对列表的修改永久有效, 若不想让函数修改列表, 可以利用切片的方式传给函数一个副本In [102]: def a(b): ...: b[0] = 1 ...: return b ...: In [103]: c = [2, 3, 4]In [104]: a(c)Out[104]: [1, 3, 4]In [105]: cOut[105]: [1, 3, 4]In [108]: d = [2, 3, 4]In [109]: a(d[:])Out[109]: [1, 3, 4]In [110]: dOut[110]: [2, 3, 4]
2、类的创建及使用
类的创建
In [131]: class A(): # 类的首字母记得大写 ...: '''测试类''' # 这里编写文档字符串, 对这个类的功能作描述 ...: ...: # 我们创建 A 的实例时,Python 将调用 A 的 __init__ 方法 ...: # self 不能少 ...: def __init__(self, a, b, c): ...: '''初始化属性''' ...: print('__init__') ...: self.a = a ...: self.b = b ...: self.c = c ...: self.d = 0 # 默认属性 ...: ...: # 记得加上 self , 不然会报错 takes 0 positional arguments but 1 was given ...: def bFunction(self): ...: print('b function') ...: ...: def cFunction(self): ...: print('c function')
类的使用
In [132]: a = A(1, 2, 3)__init__In [133]: a.aOut[133]: 1In [134]: a.bOut[134]: 2In [135]: a.cOut[135]: 3In [136]: a.dOut[136]: 0In [137]: a.bFunction()b functionIn [138]: a.cFunction()c function# 修改属性的值In [139]: a.a = 10In [140]: a.aOut[140]: 10
3、继承
In [131]: class A(): # 类的首字母记得大写 ...: '''测试类''' # 这里编写文档字符串, 对这个类的功能作描述 ...: ...: # 我们创建 A 的实例时,Python 将调用 A 的 __init__ 方法 ...: # self 不能少 ...: def __init__(self, a, b, c): ...: '''初始化属性''' ...: print('__init__') ...: self.a = a ...: self.b = b ...: self.c = c ...: self.d = 0 # 默认属性 ...: ...: # 记得加上 self , 不然会报错 takes 0 positional arguments but 1 was given ...: def bFunction(self): ...: print('b function') ...: ...: def cFunction(self): ...: print('c function') ...: # 继承的方式 In [159]: class B(A): # g 有默认值, 可以不传这个参数值 ...: def __init__(self, a, b, c, d, e, f, g = 778): ...: print('B __init__') # 初始化父类的属性 ...: super().__init__(a, b, c) ...: self.d = 10086 ...: self.e = e ...: self.f = f ...: self.g = g ...: self.h = 100 ...: # 子类重写父类的方法 ...: def cFunction(self): ...: print('B c_function') ...: ...: def dFunction(self): ...: print('B d_function') ...: ...: def eFunction(self): ...: print('B e_function')# 先初始化子类的 __init__ 方法,再调用父类的 __init__ 方法In [160]: b = B(1, 2, 3, 4, 5, 6, 7)B __init____init__# 子类有重写父类的方法则调用父类的方法In [163]: b.cFunction()B c_functionIn [164]: b.dFunction()B d_function
4、文件
http://blog.csdn.net/ztf312/article/details/47259805
5、异常
http://www.cnblogs.com/fnng/p/3518202.html
0 0
- python入门(二)
- python入门(二)
- python入门(二)
- Python 入门(二)
- Python入门(二)
- Python入门(二)
- PYTHON入门(二)
- Python入门笔记(二)
- Python入门(二,基础)
- python入门学习(二)
- python 入门笔记(二)
- python入门经典(二)
- python入门学习(二)
- Python入门基础(二)
- Python入门基础(二)
- Python入门篇(二)
- Python 快速入门(二)
- 【python入门笔记】(二)python基础
- Android编程:扇形展开的悬浮菜单按钮CircularFloatingActionMenu实例
- 万能采集大法
- 项目人力资源管理·十一
- java 调用方法栈获取类名,方法名,方法返回类型
- Mina 简单案例(客户端与服务端)
- Python 入门(二)
- 基于play的用户登陆验验证和数据库的相关连接问题
- python3.6利用pyinstall打包py为exe
- LeetCode题解–135. Candy
- AOJ 866 飞越原野 【BFS】
- 应用session对象实现登陆
- Ubuntu 16 下配置Faster_rcnn 訓練自己的数据
- Python数据采集之网页解析器
- 几种常用的矩阵范数