zoukankan      html  css  js  c++  java
  • 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__一般会给出类或者模块中定义的特性(包括属性和方法),是以字典的形式输出的使用元组来表示参数和参数值(参数,参数值或者描述)

    1 >>> list.__dict__
    2 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
    list(iterable) -> new list initialized from iterable's items"})
    list的__dict__查看

    dir()会以列表的形式给出所有可以获得的特性属性,而不含有值和属性描述:

    1 1 >>> dir(list)
    2 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']
    list使用dir()查看

    一般而言使用__dict__和dir()就可以了,当然很多时候也需要help()查看详细的文档(其实就是__doc__中的内容):

     1  help(list)
     2 Help on class list in module builtins:
     3 
     4 class list(object)
     5  |  list() -> new empty list
     6  |  list(iterable) -> new list initialized from iterable's items
     7  |
     8  |  Methods defined here:
     9  |
    10  |  __add__(self, value, /)
    11  |      Return self+value.
    12  |
    13  |  __contains__(self, key, /)
    14  |      Return key in self.
    15  |
    16  |  __delitem__(self, key, /)
    17  |      Delete self[key].
    18  |
    19  |  __eq__(self, value, /)
    20  |      Return self==value.
    21  |
    22  |  __ge__(self, value, /)
    23  |      Return self>=value.
    24  |
    25  |  __getattribute__(self, name, /)
    26  |      Return getattr(self, name).
    27  |
    28  |  __getitem__(...)
    29  |      x.__getitem__(y) <==> x[y]
    30  |
    31 -- More  --
    list使用help()查看

     如果我们现在创建了一个对象 a = list(),现在想要获取对象a有什么特性方法,也是使用dir()和help()。

    在这里需要强调的是,dir()和help()的强大之处不仅仅可以查阅类,还可以我们定义的变量。这样我们在编码过程中能够获得更多的内容。

     1 >>> a=1
     2 >>> dir(a)
     3 ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
     4 >>> help(a)
     5 Help on int object:
     6 
     7 class int(object)
     8  |  int(x=0) -> integer
     9  |  int(x, base=10) -> integer
    10  |
    11  |  Convert a number or string to an integer, or return 0 if no arguments
    12  |  are given.  If x is a number, return x.__int__().  For floating point
    13  |  numbers, this truncates towards zero.
    14  |
    15  |  If x is not a number or if base is given, then x must be a string,
    16  |  bytes, or bytearray instance representing an integer literal in the
    17  |  given base.  The literal can be preceded by '+' or '-' and be surrounded
    18  |  by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
    19  |  Base 0 means to interpret the base from the string as an integer literal.
    20  |  >>> int('0b100', base=0)
    21  |  4
    22  |
    23  |  Methods defined here:
    24  |
    25  |  __abs__(self, /)
    26  |      abs(self)
    27  |
    28  |  __add__(self, value, /)
    29  |      Return self+value.
    30  |
    31  |  __and__(self, value, /)
    32  |      Return self&value.
    33  |
    34 -- More  --
    a=1中变量a含有方法的查找

     变量和对象类型的查找等操作

    python中除了使用dir(),help(),__all__,__dict__查看原来定义设计的内容外,可以使用很多已经定义的特性来获取更多信息:

    获取变量类型和对象类型的方法:

    >>> 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__查询变量和类类型

    通过上面的代码我们发现,如果class为type其实这个本身就是一个类的定义,如果是其他的则是对象。

    你想过没有,在python中基本的变量也是对象?

    在一般语言中,我们的类型都分为整型,浮点型,字符串等等。但是在python这些类型其实都是类的形式定义的,而类也是继承了顶级超类的。

     1 >>> a = 1
     2 >>> a.__class__
     3 <class 'int'>
     4 >>> int.__class__
     5 <class 'type'>
     6 >>> str.__class__
     7 <class 'type'>
     8 >>> bool.__class__
     9 <class 'type'>
    10 >>> list.__class__
    11 <class 'type'>
    12 >>> dict.__class__
    13 <class 'type'>
    14 >>> tuple.__class__
    15 <class 'type'>
    16 >>> type.__class__
    17 <class 'type'>
    18 >>> object.__class__
    19 <class 'type'>
    20 >>> type.__bases__
    21 (<class 'object'>,)
    22 >>> int.__bases__
    23 (<class 'object'>,)
    24 >>> str.__bases__
    25 (<class 'object'>,)
    26 >>> bool.__bases__
    27 (<class 'int'>,)
    28 >>> float.__bases__
    29 (<class 'object'>,)
    30 >>> object.__bases__
    31 ()
    32 >>>
    使用__class__查看类型,__bases__查看超类

    发现没有,我们使用的类型其实都是类,除了bool继承了类int,其他的都是继承超类object。而object类没有超类。应该说,type是一个类型类!当然None类型是NoneType,而NoneType却不是类,这是说None是个空值。

    1 >>> dir(type)
    2 ['__abstractmethods__', '__base__', '__bases__', '__basicsize__', '__call__', '__class__', '__delattr__', '__dict__', '__dictoffset__', '__dir__', '__doc__', '__eq__', '__flags__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__instancecheck__', '__itemsize__', '__le__', '__lt__', '__module__', '__mro__', '__name__', '__ne__', '__new__', '__prepare__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasscheck__', '__subclasses__', '__subclasshook__', '__text_signature__', '__weakrefoffset__', 'mro']
    3 >>> dir(object)
    4 ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
    type类中含有的内容和object类中含有的内容

     is 和id()可以让我们看清变量之间的隐藏关系!

    我们都知道,python从在使用上有着弱类型的便捷性。当然在python中,相同数值的变量只有一个!这是为什么呢?这是因为他们都是指向同一个内存地址,可以将这些变量想象成他们存放的是一个个内存地址,我们对他们的赋值不过是将他们指向的内存地址做了改变!!

    从指针的角度理解python是很有帮助的。变量本身存放的是指针,如果你这个能够理解了,那么关于python的变量和内存回收机制就能够很好理解并运用了。

    A is B 操作的作用是判断A是否是B,如果为真,则说明A与B是同一个对象,如果为假,则说明A与B不是同一个对象。

    id(A) 操作的作用是判断A在内存中的id序列号。

    下面我们来详细说明下python中的相关现象:

      1.python中A和B虽然是同一个对象,但是当对A赋值之后,A与B再是同一个对象,因为python中的赋值是将A所所指向的地址改成了另一个对象的地址,这时候与B中的地址不一样,B地址所致的对象的值不会收到A赋值影响。

      2.python中同一个对象所具有的id可能是不同的,因为在没有指向该地址的变量时,python内存自动清理会清理掉这个对象。再次使用到具有相同数值的对象可能是在前一个对象自动清理之后创建的新对象

    针对第一个情况我们首先通过对True和False和数字来确定哪些值对象的id是系统自带的,即便这些值对象不被变量使用python内存清理也不会清理这些对象!

     1 >>> id(True)
     2 1538937056
     3 >>> id(False)
     4 1538937088
     5 >>> id(False) - id(True)
     6 32
     7 >>> id(-5)
     8 1539416992
     9 >>> id(-6)
    10 1667933956912
    11 >>> id(-4)
    12 1539417024
    13 >>> id(-4)-id(-5)
    14 32
    15 >>> id(-3)-id(-4)
    16 32
    17 >>> id(-3)
    18 1539417056
    19 >>> id(-2)
    20 1539417088
    21 >>> id(-2) - id(-3)
    22 32
    23 >>> id(255)
    24 1539425312
    25 >>> id(256)
    26 1539425344
    27 >>> id(256) - id(255)
    28 32
    29 >>> id(257)
    30 1667904611440
    31 >>> id(1.0)
    32 1667904643192
    通过id来判断数值和布尔值中的值对象是否是系统自带的对象

    你会发现数字-5到256是连续的,他们相邻的id值相差是32,意思是他们是32表示的数值。在不同python进程中这些相同值对象返回的id值是一致的。而小于-5或者大于256的数值,小数,超过单个字符的字符串都是python在用户使用时创建的值对象,在不同的python进程中相同的值的id是不同的!

    当然,python中还有很多对象、类、函数等是python自创建的不会因为不使用而被内存清理程序清理掉。比如 int,None,dict,list。

    不够值得一提的是 None is None是返回True 。并且id(None)的返回值是1538983120。这说明与其他脚本(比如javascript)不一样,None是空值,是一个唯一的空值对象,程序中所有的None都是相等的。都是同一个内存地址中存放的值。

    很多情况下,我们想判断两个变量是否指向同一个内存地址块存放的值,可以使用is来判断。

    python中对于全局变量,局部变量,外部变量有着额外的处理方式

    如果一个函数中定义了与外部名称相同的变量,在函数内部如何能够获得外部定义的变量呢?在其他语言中,我们都知道局部变量会覆盖掉同名的外部变量。而在python中虽然也是这个逻辑,但是他提供了 3个函数来使得我们能够获得不同作用域中定义的同名的变量值。

    globals()获取所有全局变量值

    locals()获取所有局部变量值

    nonlocals()获取所有外部变量值(因为python是支持函数嵌套的,内部函数如果想要获得外部函数局部变量的值可以使用这个)

     1 >>> a = 234
     2 >>> globals()
     3 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 234}
     4 >>> def s():
     5 ...     a = 'hello'
     6 ...     print(locals())
     7 ...
     8 >>> s()
     9 {'a': 'hello'}
    10 >>> def u():
    11 ...     a = 'world'
    12 ...     c = globals()['a']
    13 ...     print(c)
    14 ...
    15 >>> u()
    16 234
    在局部变量中获取全局变量中同名变量

    如上面代码显示的,在函数u()中a的值是‘world’而在全局变量中a的值是234,函数s()中局部变量a的值是'hello'通过globals()[变量名]就可以获得全局变量中同名的变量的值。

    1 >>> def e():
    2 ...     a = 'sdf'
    3 ...     globals()['a'] = a
    4 ...
    5 >>> e()
    6 >>> a
    7 'sdf'
    8 >>>
    局部改变上段代码中同名的全局变量值
  • 相关阅读:
    【UML】类图Class diagram(转)
    【UML】用例图Use Case diagram(转)
    【UML】概述
    JAVA设计模式之单例模式
    Java 设计模式之中介者模式
    Java设计模式之访问者模式
    Java设计模式之命令模式
    Java设计模式之责任链模式、职责链模式
    Java设计模式之迭代子模式
    npm install说明
  • 原文地址:https://www.cnblogs.com/zhangdewang/p/7236831.html
Copyright © 2011-2022 走看看