zoukankan      html  css  js  c++  java
  • python之总体理解

    作为脚本,python具备了弱类型语言的灵活性,便捷性。这在日常的开发使用中能够大幅度的减轻开发人员的编码负担,开发者也能够将精力集中在程序的逻辑管理和总体构架设计上。一般而言,随着经验的积累,开发人员都能使用python写出漂亮的代码,简洁而美观。

    python也是严谨的,从对各类预定义错误的设定我们就可以发现python具备着编译语言具备的严密的逻辑结构。可以这么讲,随着对python的深入理解,就越能感受到python在提供各类便捷操作的同时依然保持了编译语言具有的严密逻辑,只是很多“隐藏”了。

    那么,python具有着哪些值得称道的特性呢?

    (一) python具有着强有力的粘连性,能够同c,c++,java等一同工作。

    这是其他大多数语言所不具备的强有力的功能,这使得python能够同其他语言的功能代码以更好、更便捷的方式联合。将需要大量计算的部分通过编译语言来完成来提高程序的运行速度。

    (二)python具备着跨平台的特性,并支持各类通用操作

    python的强大与他的跨平台有着密不可分的联系。可以这么讲,现如今除了底层开发,如果一门语言或工具无法跨平台是很难在当下环境中获得普及与扩展的。python的优秀后台处理能力,将各类不同的平台,各类不同的工具以一致的接口方式实现了一次编写到处运行的目标(当然,部分需要适配各平台的注明)。

    (三)也可以说,python是一款优秀的软件工具!

    python通过c语言实现了核心语言,这部分包括着python的解释器。解释器起到的作用是将符合python语法规则的文本文件解释成python这款语言能够识别的指令,然后这类指令通过python这款软件实现目标功能。python的核心功能就是一个软件主体,而软件的各类功能以python规定的文本格式呈现。在大型的软件中,我们将很多关于界面配置等功能通过文本配置文件来控制,而python无疑在这方面做的更好。

    (四)python的语法设计上的优势

    python在语法设计上做了其他语言不具有的大胆开拓。

    1.python的作用于不是通过花括号和结尾的分号,而是通过缩进,这使得整体代码变得简洁而小巧。

    2.python通过字典和类实现了很多语法功能,这使得很多操作变得统一,虽然开始理解上有些别扭,但在操作上却是变得异常的简单。

      比如命名空间的实现就是通过字典来实现

    #命名空间scope
    scope ={}
    #在命名空间中预设变量 code ,值为namespace
    scope['code'] = 'namespace'
    
    #字符串中为可执行的python脚本
    pass
    #设定代码
    Sstring = """
    
    a =12
    b = 'new'
    print('hello,world')
    """
    pass
    #执行那段字符串代码,并放入命名空间scope中
     exec(Sstring,scope)

    在没有执行exec(Sstring,scope)这行代码之前,变量scope就是一个普通的字典,但是经过这行代码之后,scope虽然仍然是字典,但是具备有很多额外的键值,很显然已经被python格式化为python的命名空间了。

    python通过字典实现命名空间从而实现了变量名污染问题解决的一种方式,并且通过字典这类基础结构和通用的字典方法,初级开发人员能够很容易驾驭它。

    当然,另一些其他语言不具备的变量名污染解决方法是globals(),locals(),nonlocals()函数。这类函数分别以字典的形式提供了所有全局变量,局部变量,外部局部变量。看到这里,你相比也已经被python强大的能力所折服了吧。

      python基本的序列和字典结构其实都是python的类。

    python的列表[],元组(),字典{}其实都是类。

     1 >>> list.__bases__
     2 (<class 'object'>,)
     3 >>> tuple.__bases__
     4 (<class 'object'>,)
     5 >>> dict.__bases__
     6 (<class 'object'>,)
     7 >>> object
     8 <class 'object'>
     9 >>> object.__bases__
    10 ()
    11 >>> help(object)
    12 Help on class object in module builtins:
    13 
    14 class object
    15  |  The most base type

    通过上面我们会发现,其实在python中,很多基本都东西在python底层都是通过类的形式来实现的。所以很多类可以使用list,tuple,dict作为基类。而object是没有基类的类,也是pythond顶级超类。

    我们那list列表来说,他作为类存在通过__dict__可以获得所含有的变量

     1 >>> list.__dict__
     2 >>>{'__add__': <slot wrapper '__add__' of 'list' objects>,
     3 '__contains__': <slot wrapper '__contains__' of 'list' objects>,
     4 '__delitem__': <slot wrapper '__delitem__' of 'list' objects>,
     5 '__doc__': 'list() -> new empty list
    '
     6          'list(iterable) -> new list initialized from '
     7          "iterable's items",
     8 '__eq__': <slot wrapper '__eq__' of 'list' objects>,
     9 '__ge__': <slot wrapper '__ge__' of 'list' objects>,
    10 '__getattribute__': <slot wrapper '__getattribute__' of 'list' objects>,
    11 '__getitem__': <method '__getitem__' of 'list' objects>,
    12 '__gt__': <slot wrapper '__gt__' of 'list' objects>,
    13 '__hash__': None,
    14 '__iadd__': <slot wrapper '__iadd__' of 'list' objects>,
    15 '__imul__': <slot wrapper '__imul__' of 'list' objects>,
    16 '__init__': <slot wrapper '__init__' of 'list' objects>,
    17 '__iter__': <slot wrapper '__iter__' of 'list' objects>,
    18 '__le__': <slot wrapper '__le__' of 'list' objects>,
    19 '__len__': <slot wrapper '__len__' of 'list' objects>,
    20 '__lt__': <slot wrapper '__lt__' of 'list' objects>,
    21 '__mul__': <slot wrapper '__mul__' of 'list' objects>,
    22 '__ne__': <slot wrapper '__ne__' of 'list' objects>,
    23 '__new__': <built-in method __new__ of type object at 0x000000005BAFF530>,
    24 '__repr__': <slot wrapper '__repr__' of 'list' objects>,
    25 '__reversed__': <method '__reversed__' of 'list' objects>,
    26 '__rmul__': <slot wrapper '__rmul__' of 'list' objects>,
    27 '__setitem__': <slot wrapper '__setitem__' of 'list' objects>,
    28 '__sizeof__': <method '__sizeof__' of 'list' objects>,
    29 'append': <method 'append' of 'list' objects>,
    30 'clear': <method 'clear' of 'list' objects>,
    31 'copy': <method 'copy' of 'list' objects>,
    32 'count': <method 'count' of 'list' objects>,
    33 'extend': <method 'extend' of 'list' objects>,
    34 'index': <method 'index' of 'list' objects>,
    35 'insert': <method 'insert' of 'list' objects>,
    36 'pop': <method 'pop' of 'list' objects>,
    37 'remove': <method 'remove' of 'list' objects>,
    38 'reverse': <method 'reverse' of 'list' objects>,
    39 'sort': <method 'sort' of 'list' objects>}

    我们发现我们list中所能用到的方法append,clear,copy,count,extend,index,insert等方法都是类list的对象方法,而前面添加了双下划线的__的方法都是类的“私有方法”,这类方法很多是在我们使用通用的操作setatter,getatter时python为我们自动连接并调用的方法。而对于自定义的类想改变这些自动的功能只需要重写这部分函数即可。通过方括号[]取值,分片取值等操作就是__getitem__在起作用

    1 >>> getattr(list,'append',None)
    2 <method 'append' of 'list' objects>

      python中的类,模块,变量本质上将可以归结为内存中的功能化的地址块(指针)

    这些是脚本语言的通用方式,弱语言的本质不是不对值做类型检查和限定,而是他们在设计上为了便捷使用,而将这部分类型转换和类型判断通过隐形转换和内存指针在我们背后默默做了大量工作。正是因为所有的所有在底层指针化,我们可以通过=符号能够进行赋值、函数引用(复制指针地址),创建对象(复制创建的对象地址)等等。所以,要想了解python内部的内存回收和底层功能需要对指针,内存有着足够的理解。

  • 相关阅读:
    网站制作教程之PageAdmin网站管理系统(cms)实现多域名访问
    免费企业网站模板_学校网站模板_政府网站模板源码下载
    网站建设如何挑选自助网站建设系统
    Problem2-Project Euler
    Problem1-Project Euler
    防微杜渐——读《C安全编码标准》
    3D射影不变量
    直线匹配-LPI算法原理 Robust line matching through line–point invariants
    angular-cli 工程中使用scss文件
    JS怎么计算html标签里文字的宽度
  • 原文地址:https://www.cnblogs.com/zhangdewang/p/7231053.html
Copyright © 2011-2022 走看看