在以下示例中:
def speak(volume):
def whisper(text):
print(text.lower() + ('.' * volume))
def yell(text):
print (text.upper() + ('!' * volume))
if volume > 1:
return yell
elif volume <= 1:
return whisper
func = speak(volume=10)
func('hello')
HELLO!!!!!!!!!! # <== obviously `10` is stored in `func` somewhere
Run Code Online (Sandbox Code Playgroud)
给定func,我将如何获得“体积”?func命名空间中是否有东西可以赋予值10?我以为也许会在其中func.__globals__,func.__dict__但两者都不会。
要从 ffmpeg 输出单个帧,我可以这样做:
ffmpeg -i input.flv -ss 00:00:14.435 -vframes 1 out.png
Run Code Online (Sandbox Code Playgroud)
为了每秒输出一张图像,我可以这样做:
ffmpeg -i input.flv -vf fps=1 out%d.png
Run Code Online (Sandbox Code Playgroud)
是否有一种方法可以从这些输出中创建缩略图精灵,以帮助创建用于查找位置的缩略图的 vtt 文件?
是否有关于如何命名类变量的约定(与实例变量不同),或者没有真正的区别。例如:
import pymysql
class DB:
NUM_CONNECTIONS = 0
def __init__(self):
self.conn = None
self.cursor = None
def __repr__(self):
return 'Closed connection' if not self.conn else f'Open conection @ 0x{id(self):x}'
def connect(self):
if self.conn: return
self.conn = pymysql.connect(host='127.0.0.1',user='root')
self.cursor = self.conn.cursor()
DB.NUM_CONNECTIONS += 1
Run Code Online (Sandbox Code Playgroud)
目前我通常将变量设置为CLASS_VARIABLES和instance_variables。这有哪些常见模式?
以下类型在 BigQuery 中占用多少字节:
我的猜测是日期可以存储在 2 个字节中,时间戳可能是 8 个,但我不确定这一点,并且在https://cloud.google.com/bigquery/docs/reference/standard上没有提到-sql/数据类型页面。
执行以下操作后是否需要关闭文件:
lines = open(fname).readlines()
Run Code Online (Sandbox Code Playgroud)
还是readlines()在读取数据后关闭文件?如果没有,应该如何关闭?
__package__如果我的理解是正确的,则和变量之间的区别__name__是:
__init__.py。__name__将是完整路径,并将__package__是其目录。它是否正确?如果是这样,为什么 python 有必要在作用域中添加一个额外的变量,以便__package__有人也可以从中获取它__name__?
例如,对于一个models.py文件:
__name__==> apps.main.models__module__==> 应用程序.main