在这个问题中,出现了一个问题,可以通过改变将泛型类型参数用于关联类型的尝试来解决.这引发了一个问题"为什么相关类型在这里更合适?",这让我想知道更多.
此RFC通过以下方式阐明特征匹配:
- 将所有特征类型参数视为输入类型,和
- 提供相关类型,即输出类型.
RFC使用图形结构作为激励示例,这也在文档中使用,但我承认不完全理解相关类型版本相对于类型参数化版本的好处.主要的是该distance方法不需要关心Edge类型.这很好,但似乎有一点点关联类型的原因.
我发现在实践中使用相关类型非常直观,但在决定在我自己的API中何时何地使用它们时,我发现自己很挣扎.
在编写代码时,何时应该在泛型类型参数上选择关联类型,何时应该相反?
I discovered today that you can use and in variable assignment, similarly to how or is used. I rarely come across using or in this way, but have never even heard of people using and this way. Is this too obscure of a feature to recommend using or are there some concrete use cases where it helps with code clarity or brevity?
a = 1
b = 3
# c is equal to b unless a or b is False; …Run Code Online (Sandbox Code Playgroud) As I understand it, for it to be possible to cast sum() on an object, it must be an iterable, and must be "addable", i.e. it must implement methods __iter__ and __add__. However, when I do this for my class Point (just an example), this doesn't work.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __iter__(self):
return self
def __str__(self):
return str((self.x, self.y))
print(Point(2, …Run Code Online (Sandbox Code Playgroud) 我只是尝试将一些软件包安装到新环境中。我倾向于为每个安装指定频道,例如conda install -c <channel> <package>,而不是使用conda config --add channels <channel name>; conda install <package>. 但是,我发现某些软件包只能在同时使用多个频道时安装。这怎么工作?
我想我对包和渠道的工作方式有一个根本的误解。软件包安装如何需要多个渠道?我的理解是,特定频道托管特定包,例如conda-forgehosts x 包,它们(及其依赖项)仅使用conda-forge.
谢谢你的帮助。
我正在尝试使用python中的类添加两组坐标.这就是我到目前为止所拥有的.
class Position:
def __init__(self, x, y):
self.x = x
self.y = y
def add(self, x):
self.x = self + x
Run Code Online (Sandbox Code Playgroud)
并在一个不同的程序中运行我的课程
A = Position(1, 1)
B = Position(2, 3)
A.add(B)
A.print()
Run Code Online (Sandbox Code Playgroud)
所以我试图添加A和B来获得(3,4).我如何使用add类做到这一点?我不知道要为参数设置什么或者在函数体中放置什么以使其工作.谢谢
python ×4
python-3.x ×2
anaconda ×1
class ×1
conda ×1
function ×1
idiomatic ×1
overloading ×1
rust ×1
types ×1