python基础之入门必看操作
|
这里提供在使用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 listnlist(iterable) -> new list initialized from iterable's items"})
dir()会以列表的形式给出所有可以获得的特性属性,而不含有值和属性描述: list使用dir()查看 1 >>> dir(list) 2 ['__add__','__class__','__contains__','__delattr__','__delitem__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__','__getitem__','__gt__','__hash__','__iadd__','__imul__','__init__','__init_subclass__','__iter__','__le__','__len__','__lt__','__mul__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__reversed__','__rmul__','__setattr__','__setitem__','__sizeof__','__str__','__subclasshook__','append','clear','copy','count','extend','index','insert','pop','remove','reverse','sort'] 一般而言使用__dict__和dir()就可以了,当然很多时候也需要help()查看详细的文档(其实就是__doc__中的内容): list使用help()查看 help(list) Help on class list in module builtins: class list(object) | list() -> new empty list | list(iterable) -> new list initialized from iterable's items | | Methods defined here: | | __add__(self,value,/) | Return self+value. | | __contains__(self,key,/) | Return key in self. | | __delitem__(self,/) | Delete self[key]. | | __eq__(self,/) | Return self==value. | | __ge__(self,/) | Return self>=value. | | __getattribute__(self,name,/) | Return getattr(self,name). | | __getitem__(...) | x.__getitem__(y) <==> x[y] | -- More -- 如果我们现在创建了一个对象 a = list(),现在想要获取对象a有什么特性方法,也是使用dir()和help()。 在这里需要强调的是,dir()和help()的强大之处不仅仅可以查阅类,还可以我们定义的变量。这样我们在编码过程中能够获得更多的内容。 a=1中变量a含有方法的查找
>>> a=1
>>> dir(a)
['__abs__','__add__','__and__','__bool__','__ceil__','__divmod__','__float__','__floor__','__floordiv__','__getnewargs__','__index__','__int__','__invert__','__lshift__','__mod__','__neg__','__or__','__pos__','__pow__','__radd__','__rand__','__rdivmod__','__rfloordiv__','__rlshift__','__rmod__','__ror__','__round__','__rpow__','__rrshift__','__rshift__','__rsub__','__rtruediv__','__rxor__','__sub__','__truediv__','__trunc__','__xor__','bit_length','conjugate','denominator','from_bytes','imag','numerator','real','to_bytes']
>>> help(a)
Help on int object:
class int(object)
| int(x=0) -> integer
| int(x,base=10) -> integer
|
| Convert a number or string to an integer,or return 0 if no arguments
| are given. If x is a number,return x.__int__(). For floating point
| numbers,this truncates towards zero.
|
| If x is not a number or if base is given,then x must be a string,| bytes,or bytearray instance representing an integer literal in the
| given base. The literal can be preceded by '+' or '-' and be surrounded
| by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
| Base 0 means to interpret the base from the string as an integer literal.
| >>> int('0b100',base=0)
| 4
|
| Methods defined here:
|
| __abs__(self,/)
| abs(self)
|
| __add__(self,/)
| Return self+value.
|
| __and__(self,/)
| Return self&value.
|
-- More --
变量和对象类型的查找等操作 python中除了使用dir(),help(),__all__,__dict__查看原来定义设计的内容外,可以使用很多已经定义的特性来获取更多信息: 获取变量类型和对象类型的方法: 通过__class__查询变量和类类型 >>> a =1 >>> a.__class__ <class 'int'> >>> b = 1.0 >>> b.__class__ <class 'float'> >>> c = '' >>> c.__class__ <class 'str'> >>> d = list() >>> d.__class__ <class 'list'> >>> e = tuple() >>> e.__class__ <class 'tuple'> >>> f = dict() >>> f.__class__ <class 'dict'> >>> list.__class__ <class 'type'> >>> dict.__class__ <class 'type'> >>> tuple.__class__ <class 'type'> >>> object.__class__ <class 'type'> >>> None.__class__ <class 'NoneType'> 通过上面的代码我们发现,如果class为type其实这个本身就是一个类的定义,如果是其他的则是对象。 你想过没有,在python中基本的变量也是对象? 在一般语言中,我们的类型都分为整型,浮点型,字符串等等。但是在python这些类型其实都是类的形式定义的,而类也是继承了顶级超类的。 使用__class__查看类型,__bases__查看超类 >>> a = 1 >>> a.__class__ <class 'int'> >>> int.__class__ <class 'type'> >>> str.__class__ <class 'type'> >>> bool.__class__ <class 'type'> >>> list.__class__ <class 'type'> >>> dict.__class__ <class 'type'> >>> tuple.__class__ <class 'type'> >>> type.__class__ <class 'type'> >>> object.__class__ <class 'type'> >>> type.__bases__ (<class 'object'>,) >>> int.__bases__ (<class 'object'>,) >>> str.__bases__ (<class 'object'>,) >>> bool.__bases__ (<class 'int'>,) >>> float.__bases__ (<class 'object'>,) >>> object.__bases__ () >>> (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
