zoukankan      html  css  js  c++  java
  • python 笔记

    N Python语言把引号解释为纯字符串操作符,创建的操作符都是文字性的,单引号和双引号没有什么区别

    For的特殊用法:拆分序列

    >>> a=[c for c in 'abcdefg']

    >>> a

    ['a', 'b', 'c', 'd', 'e', 'f', 'g']

    >>> 

    按if条件拆分序列

    >>> a=[c for c in '123456' if int(c)<3]      如果if的条件为真,则执行for循环

    >>> a

    ['1', '2']

    >>> a=[c for c in '123456' if int(c)>3]      如果if的条件为假,则不执行for循环

    >>> a

    ['4', '5', '6']

    函数调用中使用星号*

    def test(*args):
        ...定义函数参数时 * 的含义又要有所不同,在这里 *args 表示把传进来的位置参数都装在元组 args 里面。比如说上面这个函数,调用 test(1, 2, 3) 的话, args 的值就是 (1, 2, 3) 。:

    def test(**kwargs):
        ...类似的, ** 就是针对关键字参数和字典的了。 调用 test(a=1,b=2,c=3) 的话, kwargs 的值就是 {'a':1,'b':2,'c':3} 了。

    在用正则表达式匹配的时候可能会只想找出匹配字符串中的一部分,可以用(?P<name>XXXX)来解决。例如提取指定后缀的文件名,但匹配时后缀也会匹配进来,这时可以用如下方法:p = re.compile(r'(?P<name>\w+).py')  p.match(str).group(name)

    (pos,)可以使结果组合成一个tuple,这样可以作(1,)+(1,2)结果为(1,1,2)

    Python中的package下面必须有一个__init__.py才能叫得上package,否则会出现importerror。对于包下面的__init__.py文件中定义的类或函数,在import包之后就可以通过级联来调用(可假设包为一个模块,即__init__)。

    命名习惯

    module_name, package_name, ClassName, method_name, ExceptionName, function_name, GLOBAL_CONSTANT_NAME, global_var_name, instance_var_name, function_parameter_name, local_var_name.

     

    py2exe

    要利用py2exe需要创建一个setup.py。setup.py内容如下

    from distutils.core import setup import py2exe
    #hello.py python script to be packaged into .exe in windows setup(console=['hello.py'])

    Notice that this is ordinary Python. Let's go through it line by line...

    1. When working with py2exe the only part of Distutils we'll typically need to reference directly is the setup function, so that's all we'll import.
    2. Once Distutils is loaded, we need to load py2exe so that it can add its command.
    3. Whitespace is good!
    4. Call setup and tell it that we want a single console application and the main entry point is "hello.py".
    The next step is to run your setup script.
    C:\Tutorial>python setup.py py2exe

    Two directories will be created when you run your setup script, build and dist. The build directory is used as working space while your application is being packaged. It is safe to delete the build directory after your setup script has finished running. The files in the dist directory are the ones needed to run your application.

    from __future__ import division

    导入python未来支持的语言特征division(精确除法),当我们没有在程序中导入该特征时,"/"操作符执行的是截断除法(Truncating Division),当我们导入精确除法之后,"/"执行的是精确除法。

    添加默认搜索路径

    在/usr/local/lib/pythonx.x/dist-packages/下添加扩展名为pth的文件,并将路径添加进去,保存即可。

    对于一个类对于len()和print怎么处理?

    可以重载内建__len__(), __repr__()内建函数

    代码缩进

    或者第一行有参数,剩下要换行的参数与第一个参数对齐,

    或者第一行不加参数,从第二行开始加参数。

    # Aligned with opening delimiter
    foo = long_function_name(var_one, var_two,
                             var_three, var_four)
    
    # More indentation included to distinguish this from the rest.
    def long_function_name(
            var_one, var_two, var_three,
            var_four):
        print(var_one)

    list[]是可变的,而tuple()和string""是不可变以,在赋值时注意传值和传址。

    可以把函数作为参数传入函数

    在对列表进行赋值处理的时候,发现l[0:0]=[]所以如果想引用某一处位置的元素不能与slice更多的一概而论,事情还不止这样,l[a:b]中的b指的是在b结束,不包括索引为b。

  • 相关阅读:
    浅谈MyBatis-Plus学习之条件构造器 EntityWrapper
    浅谈MyBatis-Plus学习之插件扩展
    [XSS防御]HttpOnly之四两拨千斤
    [PHP防火墙]输入内容存在危险字符,安全起见,已被本站拦截
    [思路笔记]WEB安全之漏洞挖掘
    通过TleChat插件一键Getshell
    云服务器上安装MSF环境
    (vshadow)Volume Shadow在渗透测试中的利用
    一个帖子csrf的例子
    yuyuecms 1.2文件删除漏洞
  • 原文地址:https://www.cnblogs.com/haoqingchuan/p/2348348.html
Copyright © 2011-2022 走看看