Python 学习笔记

最早是在大一的时候接触 Python,最初是看的廖雪峰的 Python 教程,草草学习了几天,自那以后就再也没有学过 Python 语言了。虽然没怎么学,但是用起来也不算太难,遇到不会的就查。但最近越发觉得自己的 Python 写的非常糟糕,连 import 的规则都不够清楚。在接触了一些优秀的开源项目,比如文本对抗攻击的开源库 TextAttack,看到别人写的优秀代码,激发了我系统学习 Python 语言的想法。因此有了这篇文章,记录 Python 的学习过程。

常用函数

eval 函数

eval 函数用来执行一个表达式,并返回计算结果。

>>> eval("3+10")
13
>>> z = eval("3+10*2")
>>> z
23
>>> y = eval(f"{z} ** 2 + z + 1")  # f 用来格式化字符串中的大括号 {}
>>> y
553
>>> x = eval("y + 10")
>>> x
563

基础语法

赋值、浅拷贝、深拷贝

代码示例如下,展示了赋值、浅拷贝、深拷贝三种情况对变量操作的结果。

  • 赋值:对于变量 b,它和 a 指向同一个内存地址,所以 a 改变了,b 也改变
  • 浅拷贝:对于变量 c 和 d,当 a 中的对象或 list 发生,c 和 d 也相应改变
  • 深拷贝:对于变量 e,已经和 a 没有任何关系了。
import copy

a = [[1, 2], [3, 4], [5]]

# 赋值
b = a

# 浅拷贝
c = a[:]
d = a.copy()

# 深拷贝
e = copy.deepcopy(a)

a.append([2, 4, 7])
print('a:', a)  # a: [[1, 2], [3, 4], [5], [2, 4, 7]]
print('b:', b)  # b: [[1, 2], [3, 4], [5], [2, 4, 7]]
print('c:', c)  # c: [[1, 2], [3, 4], [5]]
print('d:', d)  # d: [[1, 2], [3, 4], [5]]
print('e:', e)  # e: [[1, 2], [3, 4], [5]]

a[0].append(3)
print('a:', a)  # a: [[1, 2, 3], [3, 4], [5], [2, 4, 7]]
print('b:', b)  # b: [[1, 2, 3], [3, 4], [5], [2, 4, 7]]
print('c:', c)  # c: [[1, 2, 3], [3, 4], [5]]
print('d:', d)  # d: [[1, 2, 3], [3, 4], [5]]
print('e:', e)  # e: [[1, 2], [3, 4], [5]]

注释

我们可以参考 google 的注释规范。

类下面的注释可以带上 Attributes 的字样。

class SampleClass(object):
    """Summary of class here.

    Longer class information....
    Longer class information....

    Attributes:
        likes_spam: A boolean indicating if we like SPAM or not.
        eggs: An integer count of the eggs we have laid.
    """

    def __init__(self, likes_spam=False):
        """Inits SampleClass with blah."""
        self.likes_spam = likes_spam
        self.eggs = 0

    def public_method(self):
        """Performs operation blah."""

方法的注释可以带上 Args, Returns, Raises 的字样。

def fetch_smalltable_rows(table_handle: smalltable.Table,
                        keys: Sequence[Union[bytes, str]],
                        require_all_keys: bool = False,
) -> Mapping[bytes, Tuple[str]]:
    """Fetches rows from a Smalltable.

    Retrieves rows pertaining to the given keys from the Table instance
    represented by table_handle.  String keys will be UTF-8 encoded.

    Args:
        table_handle: An open smalltable.Table instance.
        keys: A sequence of strings representing the key of each table
        row to fetch.  String keys will be UTF-8 encoded.
        require_all_keys: Optional; If require_all_keys is True only
        rows with values set for all keys will be returned.

    Returns:
        A dict mapping keys to the corresponding table row data
        fetched. Each row is represented as a tuple of strings. For
        example:

        {b'Serak': ('Rigel VII', 'Preparer'),
        b'Zim': ('Irk', 'Invader'),
        b'Lrrr': ('Omicron Persei 8', 'Emperor')}

        Returned keys are always bytes.  If a key from the keys argument is
        missing from the dictionary, then that row was not found in the
        table (and require_all_keys must have been False).

    Raises:
        IOError: An error occurred accessing the smalltable.
    """

面向对象

类的定义

python3.x 中所有的类默认继承自 object 基类,以下的三种写法是一样的。

class Apple(object):
    pass

class Apple():
    pass

class Apple:
    pass

类的预定义函数

class Apple(object):

    def __init__(self, size) -> None:
        """返回值默认是 None
        """
        self.size = size

    def __str__(self) -> str:
        """可读性:对象表示成字符串的方法,str 或者 print 的时候会自动调用
        """
        return 'apple: ' + str(self.size)

    def __repr__(self) -> str:
        """可还原性:对象表示成可还原为原对象的字符串
        """
        return 'apple: ' + str(self.size)

抽象基类和抽象方法

使用 ABC 类作为抽象基类,用 @abstractclassmethod 注解抽象方法,抽象基类不能实例化。

from abc import ABC, abstractclassmethod


class SentenceAugmenter(object):

    def __init__(self) -> None:
        super().__init__()

    @abstractclassmethod
    def augment(self, text):
        pass

单例模式

通过覆盖 __new__ 方法,在创建对象的时候,初始化一个对象。点这里看更多的例子。

class Single(object):

    _instance = None
    
    def __new__(cls, *args, **kw):
        if cls._instance is None:
            cls._instance = object.__new__(cls, *args, **kw)
        return cls._instance
    
    def __init__(self):
        pass

single1 = Single()
single2 = Single()
print(id(single1) == id(single2))

其他

python 导入路径

打印当前的导入路径,将自定义的包加入到路径前。

import sys
print(sys.path)

sys.path = ['/path/to/custom/package'] + sys.path

修改 PYTHONPATH 环境变量可以修改导入路径,路径中的包可以覆盖 pip 安装在 site-packages 里面的包。通过修改 PYTHONPATH 可以用来开发 Python 包。

export PYTHONPATH=$PYTHONPATH:/path/to/custom/package

命令行执行 python 代码

通过执行下面代码,可以直接下载 nltk 需要的依赖。

python -c "import nltk; nltk.download('omw-1.4')"

虚拟环境

# 创建环境目录文件夹
python3 -m venv /venv/tvm

# 进入目标目录,激活环境
cd /venv/tvm/
source ./bin/activate

# 退出环境
deactivate
posted @ 2023-01-10 21:42  楷哥  阅读(202)  评论(0编辑  收藏  举报