我有两个带有注释/类型提示的类。
第一个工作没有任何问题:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from bunyamin.models.exchange import Exchange
class Kline:
def read_klines(exchange: Exchange):
pass
Run Code Online (Sandbox Code Playgroud)
第二个确实很相似:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from bunyamin.models.timeframe import Timeframe
def normalize_dt(dt: datetime, timeframe: Timeframe) -> datetime: # -> This line raises NameError
pass
Run Code Online (Sandbox Code Playgroud)
但提出NameError: name 'Timeframe' is not defined。
我知道我可以使用字符串(如'Timeframe')而不是类本身,但据我所知这不是预期的行为。我缺少什么?
我使用的 Python 版本是 3.8.2(如果相关的话)。
编辑:
当我试图隔离问题时,我省略了所有“看似不相关”的导入。但第一个文件实际上包含from __future__ import annotations在顶部,这使得它可以工作。详情请参阅第一个答案。
我正在尝试启动并运行一个 django 项目,该项目依赖于 GDAL 库。我正在开发基于 M1 的 mac。
按照官方 Django 文档上的说明,我已经通过brew 安装了必要的包
$ brew install postgresql
$ brew install postgis
$ brew install gdal
$ brew install libgeoip
Run Code Online (Sandbox Code Playgroud)
gdalinfo --version运行良好并显示版本为3.3.1
gdal-config --libs返回此路径:-L/opt/homebrew/Cellar/gdal/3.3.1_2/lib -lgdal
符号链接也放置在自制程序的 lib 目录中,该目录位于我的路径环境变量中。
当我尝试在不指定 gdal 库路径的情况下运行 django 时,它抱怨找不到 GDAL 包(即使该库是可访问的,因为它的符号链接可通过路径环境变量获得)。
当我尝试使用 指定 GDAL 库的路径时GDAL_LIBRARY_PATH,出现以下错误:
OSError: dlopen(/opt/homebrew/Cellar/gdal/3.3.1_2/lib/libgdal.dylib, 6): no suitable image found. Did find:
/opt/homebrew/Cellar/gdal/3.3.1_2/lib/libgdal.dylib: mach-o, but wrong architecture
/opt/homebrew/Cellar/gdal/3.3.1_2/lib/libgdal.29.dylib: mach-o, but wrong architecture
Run Code Online (Sandbox Code Playgroud)
PS我已经看过这个答案,但没有帮助。
当我尝试运行 gdalinfo 时,它运行良好,但是当 django …
我开始学习C/C++的套接字编程,并检查手册页中的bind,listen等函数.
当我在手册页之间导航时,我注意到有相同系统调用有多个手册页的情况,例如socket()
http://man7.org/linux/man-pages/man2/socket.2.html
http://man7.org/linux/man-pages/man3/socket.3p.html
http://man7.org/linux/man-pages/man7/socket.7.html
在这些手册中,我的linux盒子里出现的是第一个(socket(2)).
我注意到带有3p后缀的标题为"POSIX程序员手册",其余两个标题为"Linux程序员手册".函数原型和用法是相同的(据我所知).
我的问题是:同一系统调用的两个不同版本的Linux程序员手册的目的是什么,并且paranthesis意味着什么(socket(2),socket(3p),socket(7))?
想象一下我有这两个结构:
type Game struct {
Name string
MultiplayerSupport bool
Genre string
Version string
}
type ERP struct {
Name string
MRPSupport bool
SupportedDatabases []string
Version string
}
Run Code Online (Sandbox Code Playgroud)
我想要一个附加到这些结构的函数来打印Version变量
func (e *ERP) PrintVersion() {
fmt.Println("Version is", e.Version)
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用接口,但我仍然必须为这两个结构定义两个相同的函数,这是代码重复。
防止代码重复的最佳实践是什么?
PS 在用“这个问题已经有一个答案”标记它之前,它不是同一个问题,因为在下面的问题中,结构之间的字段名称不同。
python ×2
python-3.x ×2
annotations ×1
apple-m1 ×1
c ×1
c++ ×1
django ×1
gdal ×1
go ×1
linux ×1
manpage ×1
methods ×1
sockets ×1
struct ×1
type-hinting ×1