小编Qua*_*ris的帖子

何时使用关联类型与泛型类型是否合适?

这个问题中,出现了一个问题,可以通过改变将泛型类型参数用于关联类型的尝试来解决.这引发了一个问题"为什么相关类型在这里更合适?",这让我想知道更多.

引入相关类型RFC说:

此RFC通过以下方式阐明特征匹配:

  • 将所有特征类型参数视为输入类型,和
  • 提供相关类型,即输出类型.

RFC使用图形结构作为激励示例,这也在文档中使用,但我承认不完全理解相关类型版本相对于类型参数化版本的好处.主要的是该distance方法不需要关心Edge类型.这很好,但似乎有一点点关联类型的原因.

我发现在实践中使用相关类型非常直观,但在决定在我自己的API中何时何地使用它们时,我发现自己很挣扎.

在编写代码时,何时应该在泛型类型参数上选择关联类型,何时应该相反?

types idiomatic rust

84
推荐指数
2
解决办法
8093
查看次数

Use cases for "and" in variable assignment

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)

python variable-assignment python-3.x

3
推荐指数
1
解决办法
86
查看次数

Implementing sum() for custom datatype

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)

python overloading custom-data-type python-3.x

2
推荐指数
1
解决办法
37
查看次数

anaconda 包如何需要两个通道才能安装?

我只是尝试将一些软件包安装到新环境中。我倾向于为每个安装指定频道,例如conda install -c <channel> <package>,而不是使用conda config --add channels <channel name>; conda install <package>. 但是,我发现某些软件包只能在同时使用多个频道时安装。这怎么工作?

我想我对包和渠道的工作方式有一个根本的误解。软件包安装如何需要多个渠道?我的理解是,特定频道托管特定包,例如conda-forgehosts x 包,它们(及其依赖项)仅使用conda-forge.

谢谢你的帮助。

python anaconda conda

1
推荐指数
1
解决办法
2648
查看次数

使用类在Python中添加两组坐标?

我正在尝试使用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 class function

0
推荐指数
1
解决办法
3976
查看次数