小编Que*_*low的帖子

_.debounce 检查去抖是否待处理

有什么方法可以检查去抖功能是否正在等待处理?

通过使用源码分析我发现只有两种方法:flushcancel

javascript lodash

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

检查Python中函数的参数

例如,我有一个产生程序噪音的函数

def procedural_noise(width, height, seed):
   ...
Run Code Online (Sandbox Code Playgroud)

该功能的所有参数都应为正.我想,如果on参数是非正的,我需要检查它并抛出异常.它是一种好的(pythonic方式)方法吗?

让我们假设,我是对的.哪个是检查参数的最佳方法?


我可以为每个参数编写检查器:

def procedural_noise(width, height, seed):
    if width <= 0:
        raise ValueError("Width should be positive")
    if height <= 0:
        raise ValueError("Height should be positive")
    if seed <= 0:
        raise ValueError("Seed should be positive")
    ...
Run Code Online (Sandbox Code Playgroud)

对于程序员来说,当他得到例外时应该清楚,他应该纠正什么,但在我看来这并不好看.

以下代码更容易,但它与理想情况相差太远:

def procedural_noise(width, height, seed):
    if width <= 0 or height <= 0 or seed <= 0:
        raise ValueError("All of the parameters should be positive")
    ...
Run Code Online (Sandbox Code Playgroud)

最后一个问题:哪个是用unittest框架编写测试的最佳方法,它检查参数类型及其值?

我可以在测试类中编写以下函数:

def test_positive(self):
    self.assertRaises(ValueError, main.procedural_noise, -10, -10, 187)
Run Code Online (Sandbox Code Playgroud)

这是正确的解决方案吗? …

python function

4
推荐指数
2
解决办法
2222
查看次数

具有可变数量的参数的函数,没有任何显式参数

如何在C++的工作与功能的格式如下:void function(...) {}

真的需要至少一个隐含参数吗?

c++ function

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

CSS中的奇怪继承

我有以下代码:

<html>
<head>
<style>
    table {
        border: solid red 2px;
    }
    tr {
        border: solid red 2px;
    }
    td {
        border: inherit;
    }
</style>
</head>
<body>
    <table>
    <tr>
        <td> 1 </td> <td> 2 </td>
    </tr>
    <tr>
        <td> 1 </td> <td> 2 </td>
    </tr>
    </table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

它工作正常."tr"的属性边界由子"td"继承.

但是这个代码的工作方式不同,尽管如此,工作的逻辑是相同的:

<html>
<head>
<style>
    table {
        border: solid red 2px;
    }
    tr {
        border: inherit;
    }
    td {
        border: inherit;
    }
</style>
</head>
<body>
    <table>
    <tr>
        <td> 1 </td> <td> 2 </td>
    </tr> …
Run Code Online (Sandbox Code Playgroud)

html css inheritance

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

标签 统计

function ×2

c++ ×1

css ×1

html ×1

inheritance ×1

javascript ×1

lodash ×1

python ×1