定义一个函数,
MyFunction(argument, *args): [为*args 中的arg 对argument[arg] 做一些事情]
如果 *args 为空,则该函数不执行任何操作,但我想设置默认行为“如果 *args == 0 的长度则使用整个集合”
def Export(source, target, *args, sep=','):
for item in source:
SubsetOutput(WriteFlatFile(target), args).send(item[0])
Run Code Online (Sandbox Code Playgroud)
我不想在每次迭代中检查 args 的长度,并且在迭代开始之前我无法访问源中 item 的键......
所以,我可以
if len(args) != 0:
for item in source:
else
for item in source:
Run Code Online (Sandbox Code Playgroud)
这可能会起作用,但似乎不够“pythonic”?
这是(是否存在)一种标准方法来处理 *args 或 **kwargs 以及当其中一个为空时的默认行为?
更多代码:
def __coroutine(func):
"""
a decorator for coroutines to automatically prime the routine
code and method from 'curous course on coroutines and concurrency'
by david beazley www.dabeaz.com
"""
def __start(*args, …Run Code Online (Sandbox Code Playgroud) 我想使用functools.partial将某个参数设置为常量,同时完全删除该参数。
让我用一个简单的例子来解释它。
from functools import partial
def f(a, b):
return a * b
g = partial(f, b=2)
Run Code Online (Sandbox Code Playgroud)
但是,此函数g仍然具有以下调用签名:
g?
Signature: g(a, *, b=1)
Call signature: g(*args, **kwargs)
Type: partial
String form: functools.partial(<function f at 0x7ff7045289d8>, b=1)
File: /opt/conda/envs/dev/lib/python3.6/functools.py
Docstring:
partial(func, *args, **keywords) - new function with partial application
of the given arguments and keywords.
Run Code Online (Sandbox Code Playgroud)
我当然可以使用lambda函数来做到这一点,例如:
def f(a, b):
return a * b
g = lambda a: f(a, b=2)
Run Code Online (Sandbox Code Playgroud)
具有正确的呼叫签名:
g?
Signature: g(a)
Docstring: <no docstring>
File: …Run Code Online (Sandbox Code Playgroud) 我注意到PHP手册中存在一些不一致之处; 记录了许多核心功能签名以通过引用接受参数,但是它们按值接受参数.
我之前发布了一个更具体的问题,@ cweiske提供了一个很好的答案(参考了相关的PHP源代码)但是这些不一致似乎更加猖獗.
有许多功能受此影响(我将更新此列表作为保证;另请注意,这些测试是在error_reporting(-1)环境中完成的)
array array_replace_recursive ( array &$array , array &$array1 [, array &$... ] )$array,$array1等等,通过价值.这已得到纠正.bool array_multisort ( array &$arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $... ]]] ) $arr按值接受参数等.这应该抛出一个错误,因为如果参数不是变量,它将不会做任何事情.现在我很担心,不是因为我对文档很苛刻,而是因为我担心PHP开发人员会对这些函数的实现细节(或同样不可靠的东西)进行讨论.
我使用array_replace_recursive()例如,获取数组参数,并将其应用于包含默认值的另一个数组.我的一些代码库利用了这种不一致性,只需:
$values = array_replace_recursive(array(
'setting_1' => array(
'sub-setting_1' => '',
'sub-setting_2' …Run Code Online (Sandbox Code Playgroud) 说我有
struct mystruct
{
};
Run Code Online (Sandbox Code Playgroud)
是否有区别:
void foo(struct mystruct x){}
Run Code Online (Sandbox Code Playgroud)
和
void foo(mystruct x){}
Run Code Online (Sandbox Code Playgroud)
?
在python中有一个有趣且非常有用的工具,您可以通过它来模拟函数签名上的元组的值.
def first((a, b)):
return a
x = (4, 9)
first(x)
li = [(5, 4), (8, 9)]
map(first, li)
def second(a, b):
# does not work the same way
return b
Run Code Online (Sandbox Code Playgroud)
我没有看到任何有关此用途的文献.python社区用于此的词汇是什么?有没有令人信服的理由不使用它?
python tuples pattern-matching python-2.7 function-signature
假设你有一个仿函数:
struct MyFunctor
{
bool operator ()( int value )
{
return true;
}
};
Run Code Online (Sandbox Code Playgroud)
是否可以检索仿函数成员的参数类型以便在模板中使用?以下是这个神话功能的用法:
template < typename FunctorType >
bool doIt( FunctorType functor, typename FunctorType::operator()::arg1 arg )
{
return functor( arg );
}
Run Code Online (Sandbox Code Playgroud)
是否有一种有效的语法可以替代我的神话FunctorType::operator()::arg1?
我正在尝试编写一些功能,我需要保存不同的函数,然后提取它们的参数类型.所以我使用函数签名作为模板参数.但我得到一些意想不到的结果.这是代码:
#include <functional>
#include <iostream>
template <class T>
struct foo
{
foo()
{
std::cout << "class T" << std::endl;
}
};
template <class Ret, class Arg>
struct foo<Ret(Arg)>
{
foo()
{
std::cout << "Ret(Arg)" << std::endl;
}
};
template <class T>
void save(std::function<T>)
{
new foo<T>();
}
int main(int argc, char* argv[])
{
std::function<void(void)> someFoo;
save(someFoo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因此,如果变量someFoo是具有类型的函数void(void),则它实例化第一个模板,foo<T>.但是,如果我将其更改为void(int),则会实现所需的专用模板.这是为什么?
假设有腿动物有一个特征:
trait Legged {
val legs: Int
def updateLegs(legs: Int): Legged
}
Run Code Online (Sandbox Code Playgroud)
有两种这样的有腿的动物:
case class Chicken(feathers: Int, legs: Int = 2) extends Legged {
override def updateLegs(legs: Int): Legged = copy(legs = legs)
}
case class Dog(name: String, legs: Int = 4) extends Legged {
override def updateLegs(legs: Int): Legged = copy(legs = legs)
}
Run Code Online (Sandbox Code Playgroud)
在农场里还有这些动物的持有人
case class Farm(chicken: Chicken, dog: Dog)
Run Code Online (Sandbox Code Playgroud)
以及一种通过添加一条额外的腿来变异所有有腿动物的通用方法
def mutate(legged: Legged): Legged = legged.updateLegs(legged.legs + 1)
Run Code Online (Sandbox Code Playgroud)
问题是如何在 上实现一个方法,Farm以便将mutate: Legged => Legged函数作为参数并将其应用于所有 …
我有一个函数可以使用 HsOpenSsl 的函数读取 Rsa 密钥,readPrivateKey不幸的是我的函数的签名是 this String -> IO (Maybe (IO Maybe RsaKey))。我需要 PEM 格式和 Cryptonite.RSA 密钥,并且我编写了函数mkRsaKey来从 PEM 格式的字符串中生成密钥。
代码如下:
import qualified Crypto.PubKey.RSA as Rsa --from cryptonite
import OpenSSL.EVP.PKey -- from HsOpenSSL
import OpenSSL.PEM -- from HsOpenSSL
import OpenSSL.RSA -- from HsOpenSSL
import Prelude
data RsaKey = RsaKey
{ rsaKeyCryptoniteKey :: Rsa.PrivateKey,
rsaKeyStringRepr :: String
}
deriving (Show)
openSslKeyToCryptoniteKey :: RSAKeyPair -> Maybe Rsa.PrivateKey
openSslKeyToCryptoniteKey key = do
let d = rsaD key
let p …Run Code Online (Sandbox Code Playgroud) 让我说我有
use v5.026;
use feature 'signatures';
sub foo ($opt1, $opt2) {
say $opt1 if $opt2;
}
main::foo(1,2);
main::foo(1);
Run Code Online (Sandbox Code Playgroud)
现在我想foo使用和不使用opt2 进行呼叫:
foo(1); # not currently accepted
foo(1,2); # works fine
Run Code Online (Sandbox Code Playgroud)