Skip to content

Instantly share code, notes, and snippets.

@YimianDai
Last active September 19, 2019 22:00
Show Gist options
  • Save YimianDai/4fa546cf51e648063085c022cd22d0ed to your computer and use it in GitHub Desktop.
Save YimianDai/4fa546cf51e648063085c022cd22d0ed to your computer and use it in GitHub Desktop.
Notes on Python
  1. Python Syntax
  2. Python Environment
  3. Python Snippets

Python Syntax

函数注释 Function Annotations

原来的 Python 变量是没有类型的,这个 Function Annotations 其实是为了规范函数中参数的类型,给调用者提供明显的注释,已在运行前对参数类型做出检查,免得运行时,报 ErrorValue 错误。除了 Function Annotations 外,还有一个检查变量类型的方法是 isinstance 方法。

7.3 给函数参数增加元信息 给出了一个最简单的例子

def add(x:int, y:int) -> int:
    return x + y

冒号 : 后面是变量类型,箭头 -> 后面是返回的变量类型

Python Environment

Notes on Python Virtual Env

Python Snippets

Path and Files

List all files of a directory?
Solution 1
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
Solution 2
from os import walk

f = []
for (dirpath, dirnames, filenames) in walk(mypath):
    f.extend(filenames)
    break

by How do I list all files of a directory?

Import Files From Grand-grandParent's Directory
import sys

sys.path.append('....')

from data.flir.detection import FLIRDetection

如果只是 parent, 那么可以用 from ..data.flir.detection import FLIRDetection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment