Python基础:函数、类、模块
2011-12-15 13:08
621 查看
函数
函数声明:
返回值:如果没有显示的返回值,返回值为None
参数传递:形参都是引用传递,意味着在函数内部对形参的改变都会改变实参的值;当然也有例外,如果实参是不可以改变的对象,如字符串、元组,则是值传递(记住Python中所有的类型皆为对象)
[b]一些有用的函数:
[/b]
类
类的声明:
__init__函数:类似与C++中的constructor,但是又不完全像C++中的constructor函数。__init__函数只是对象在实例话之后第一个调用的函数,与C++中constructor函数对应的函数应该是__new__函数。
[b]创建类的实例:[/b]
模块
导入模块:
调用模块内的函数或者访问变量:
函数声明:
def function_name([arguments]): "optional documentation string" function_suite
返回值:如果没有显示的返回值,返回值为None
参数传递:形参都是引用传递,意味着在函数内部对形参的改变都会改变实参的值;当然也有例外,如果实参是不可以改变的对象,如字符串、元组,则是值传递(记住Python中所有的类型皆为对象)
[b]一些有用的函数:
[/b]
Function | Description |
dir([obj]) | Display attributes of object or the names of global variables if no parameter given |
help([obj]) | Display object's documentation string in a pretty-printed format or enters interactive help if no parameter given |
int(obj) | Convert object to an integer |
len(obj) | Return length of object |
open(fn, mode) | Open file fn with mode ('r' = read, 'w' = write) |
range([[start, ]stop[,step]) | Return a list of integers that begin at start up to but not including stop in increments of step; start defaults to 0, and step defaults to 1 |
raw_input(str) | Wait for text input from the user, optional prompt string can be provided |
str(obj) | Convert object to a string |
type(obj) | Return type of object (a type object itself!) |
类的声明:
class ClassName (base_class[es]): "optional documentation string" static_member_declarations method_declarations
__init__函数:类似与C++中的constructor,但是又不完全像C++中的constructor函数。__init__函数只是对象在实例话之后第一个调用的函数,与C++中constructor函数对应的函数应该是__new__函数。
[b]创建类的实例:[/b]
foo1 = FooClass()
模块
导入模块:
import module_name
调用模块内的函数或者访问变量:
module.function() module.variable
相关文章推荐
- 学习笔记☞ python 基础(python3)【四】(函数 function,None,return,列表-2,random模块)
- Python基础语法05--函数模块
- Python基础教程之函数与模块
- python基础学习之os、os.path模块常用函数
- python基础2 (函数,模块,文件I/O,异常处理)
- Python基础教程(七):函数、模块
- Python基础:数值类型、常用函数、工具模块以及bool类型本质
- Python 第三篇(上):python文件基础操作、json模块、lambda、map、filter、reduce和函数位置参数
- 基础入门_Python-模块和包.为wsgiref实现的WSGI服务器编写处理函数?
- python基础知识之变量、语句、函数、模块
- python基础:os模块中关于文件/目录常用的函数使用方法
- Python基础:数据类型、变量定义、输入/输出、逻辑、函数/模块(导入)/类、异常处理
- Python基础(六)——重用代码:函数、模块、方法
- Python程序设计基础 函数与模块习题
- Python基础学习----模块与函数
- python中使用sys模板和logging模块获取行号和函数名的方法
- python time模块常用函数
- python 发送邮件函数模块
- 【Python基础学习篇】Python模块和包
- python3 基础模块(随机数,bisect,匹配统计,压缩,csv,logging,getpass,configparser等模块)