首页 > 编程 > Python > 正文

python基础之入门必看操作

2020-02-16 01:58:04
字体:
来源:转载
供稿:网友

这里提供在使用python进行开发中常使用到的方法技巧,如有不对欢迎批评指正。

要点:开发中类、变量特性查询,类型就是类,断言的使用,深浅复制判断等

python脚本文件是使用UTF-8编码的,所以在发现中文字符出现乱码时应当考虑是否文本文件采用UTF-8编码。

如果想指定不同的编码需要在源码文件中开头处添加这样的注释:

# -*- coding: utf-8 -*-

如果python在linux和unix系统中运行,需要在源码的第一行添加:

#!/usr/bin/python3

如何获取python中各个模块,变量,类等的内容呢?

python中关于帮组的查询可以通过变量__all__,__dict__,函数help(),dir()来获取。

如果__all__在类,模块中定义,一般包含着外部可以调用的特性,如果定义了没有赋值,会自动使用非下划线开头的特性填充。如果没有参数__all__python会提示AttributeError属性错误。

__dict__一般会给出类或者模块中定义的特性(包括属性和方法),是以字典的形式输出的使用元组来表示参数和参数值(参数,参数值或者描述)

list的__dict__查看

>>> list.__dict__2 mappingproxy({'__repr__': <slot wrapper '__repr__' of 'list' objects>, '__hash__': None, '__getattribute__': <slot wrapper '__getattribute__' of 'list' objects>, '__lt__': <slot wrapper '__lt__' of 'list' objects>, '__le__': <slot wrapper '__le__' of 'list' objects>, '__eq__': <slot wrapper '__eq__' of 'list' objects>, '__ne__': <slot wrapper '__ne__' of 'list' objects>, '__gt__': <slot wrapper '__gt__' of 'list' objects>, '__ge__': <slot wrapper '__ge__' of 'list' objects>, '__iter__': <slot wrapper '__iter__' of 'list' objects>, '__init__': <slot wrapper '__init__' of 'list' objects>, '__len__': <slot wrapper '__len__' of 'list' objects>, '__getitem__': <method '__getitem__' of 'list' objects>, '__setitem__': <slot wrapper '__setitem__' of 'list' objects>, '__delitem__': <slot wrapper '__delitem__' of 'list' objects>, '__add__': <slot wrapper '__add__' of 'list' objects>, '__mul__': <slot wrapper '__mul__' of 'list' objects>, '__rmul__': <slot wrapper '__rmul__' of 'list' objects>, '__contains__': <slot wrapper '__contains__' of 'list' objects>, '__iadd__': <slot wrapper '__iadd__' of 'list' objects>, '__imul__': <slot wrapper '__imul__' of 'list' objects>, '__new__': <built-in method __new__ of type object at 0x000000005BBAF530>, '__reversed__': <method '__reversed__' of 'list' objects>, '__sizeof__': <method '__sizeof__' of 'list' objects>, 'clear': <method 'clear' of 'list' objects>, 'copy': <method 'copy' of 'list' objects>, 'append': <method 'append' of 'list' objects>, 'insert': <method 'insert' of 'list' objects>, 'extend': <method 'extend' of 'list' objects>, 'pop': <method 'pop' of 'list' objects>, 'remove': <method 'remove' of 'list' objects>, 'index': <method 'index' of 'list' objects>, 'count': <method 'count' of 'list' objects>, 'reverse': <method 'reverse' of 'list' objects>, 'sort': <method 'sort' of 'list' objects>, '__doc__': "list() -> new empty list/nlist(iterable) -> new list initialized from iterable's items"})            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表