小编Enn*_*ael的帖子

Python - 类型提示中的花括号

这些是什么意思?

 def f(a: {int, float}):
    pass
Run Code Online (Sandbox Code Playgroud)

我在通过PyCharm获取文档时看到了一些标准Python模块中使用的这种语法,我不知道它意味着什么.a我的例子中的暗示类型是什么?我可以将哪些类型传递给此功能?

其中I已经看到了这个特定的例子是在tkinterFrame __init__方法,其中该master参数的类型的{tk, _w}.

python type-hinting pycharm

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

替换std :: array的C++

std::array<...>如果我不想提供constexpr尺寸,最好的替代品是什么?我认为最好只使用它std::vector并做reserve(...)它,但也许我忽略了什么?

c++ arrays vector

8
推荐指数
2
解决办法
1028
查看次数

C++ 14我应该多久使用一次constexpr?

我一直在读一本关于C++ 14/11的书.我刚刚读完了关于该constexpr关键字的章节.我知道它用于什么,但我应该多久使用一次constexpr?即使在我知道永远不会用于创建contstexpr对象的类的代码中,我是否应该使用它?(以防万一,因为它不会让我付出任何代价,对吧?)

c++ constexpr c++14

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

Visual Studio代码 - JavaScript文件的类型提示

是否可以将类型信息提供给VS Code linter以获取JavaScript文件?我有一个JavaScript文件和一个.d.ts文件,我希望使用此.d.ts文件解析JavaScript文件的类型错误.那可能吗?如果不能直接在VS Code中使用,是否可以使用其他工具?

要添加示例:

// file.d.ts
declare function f(x: number): number;

// file.js
function f(x) { return x * 2; }
f('Not a number'); // This should be an error
Run Code Online (Sandbox Code Playgroud)

javascript visual-studio-code

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

对类的所有成员调用 __exit__

有没有一种 Pythonic 的方式来自动自动__exit__处理一个类的所有成员?

class C:
    def __init__(self):
        self.a = open('foo')
        self.b = open('bar')

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        # Is it correct to just forward the parameters here?
        self.a.__exit__(self, exc_type, exc_value, traceback)
        self.b.__exit__(self, exc_type, exc_value, traceback)
Run Code Online (Sandbox Code Playgroud)

我能做到这一点,而无需手动调用__exit__ab?我什至__exit__正确调用吗?

假设我拥有的资源file不像示例中那样,并且没有像closeor那样的方法destroy。在__enter__and之上实现这样的方法可能是一种好习惯__exit__吗?

python python-3.x

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

`dyn` 和泛型有什么区别?

我正在阅读一些代码,它有一个consume函数可以让我传递我自己的函数f

fn consume<R, F>(self, _timestamp: Instant, len: usize, f: F) -> Result<R>
    where
        F: FnOnce(&mut [u8]) -> Result<R>,
Run Code Online (Sandbox Code Playgroud)

我写了一些类似的代码,但像这样:

pub fn phy_receive(
        &mut self,
        f: &mut dyn FnMut(&[u8])
    ) -> u8 {
Run Code Online (Sandbox Code Playgroud)

公平地说,除了FnOncevs之外,我不知道有什么区别FnMut。使用dyn与泛型类型参数来指定此函数有什么区别?

rust

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

为什么我需要先固定一个 future,然后才能等待对它的引用?

tokio教程的select!状态:

需要注意的是,要等待引用,被引用的值必须被固定或实现 Unpin。

事实上,以下代码无法编译:

let fut = example(); // example is an async fn
(&mut fut).await;
Run Code Online (Sandbox Code Playgroud)

并出现以下错误消息:

error[E0277]: `from_generator::GenFuture<[static generator@src/main.rs:15:27: 17:2]>` cannot be unpinned
... snip ...
within `impl futures::Future<Output = i32>`, the trait `Unpin` is not implemented for `from_generator::GenFuture<[static generator@src/main.rs:15:27: 17:2]>
... snip ...
note: consider using `Box::pin`
Run Code Online (Sandbox Code Playgroud)

固定未来可以解决这个问题:

error[E0277]: `from_generator::GenFuture<[static generator@src/main.rs:15:27: 17:2]>` cannot be unpinned
... snip ...
within `impl futures::Future<Output = i32>`, the trait `Unpin` is not implemented for `from_generator::GenFuture<[static generator@src/main.rs:15:27: 17:2]>
... snip …
Run Code Online (Sandbox Code Playgroud)

future rust async-await

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

C++ - 为什么在op + =方面实现op +而不是相反?

为什么这个实现:

T& T::operator+=( const T& ) {
  // ... implementation ...
  return *this;
}

T operator+( const T& lhs, const T& rhs ) {
  T temp( lhs );
  return temp += rhs;
}
Run Code Online (Sandbox Code Playgroud)

比这更频繁地传播:

T& T::operator+=( const T& rhs ) {
  *this = *this + rhs;
  return *this;
}

T operator+( const T& lhs, const T& rhs ) {
  // ... implementation ...
  return some_result;
}
Run Code Online (Sandbox Code Playgroud)

是否有任何理由,或者只是一个随机的巧合,我看到人们在我阅读的文献中多次这样实现它,而从来没有反过来?

c++ code-reuse operator-overloading

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