标签: arguments

基于Python类的装饰器,其参数可以修饰方法或函数

我见过很多Python装饰器的例子:

  • 函数样式装饰器(包装函数)
  • 类风格装饰(实施__init__,__get____call__)
  • 不带参数的装饰器
  • 带参数的装饰器
  • "方法友好"的装饰器(即可以在类中装饰方法)
  • "函数友好"的装饰器(可以装饰普通函数
  • 可以装饰方法和功能的装饰器

但我从来没有见过一个可以完成上述所有工作的例子,而且我很难从特定问题的各种答案中综合出来(比如这一个,这一个,或者这一个(它有一个最好的答案)我曾见过SO)),如何结合以上所有内容.

我想要的是一个基于类的装饰器,它可以装饰方法或函数,并且至少需要一个额外的参数.即以下内容可行:

class MyDecorator(object):
    def __init__(self, fn, argument):
        self.fn = fn
        self.arg = argument

    def __get__(self, ....):
        # voodoo magic for handling distinction between method and function here

    def __call__(self, *args, *kwargs):
        print "In my decorator before call, with arg %s" % self.arg
        self.fn(*args, **kwargs)
        print "In my decorator after call, with arg %s" …
Run Code Online (Sandbox Code Playgroud)

python methods arguments function decorator

39
推荐指数
3
解决办法
2万
查看次数

读取命名的命令参数

我可以argparse用来读取不需要按特定顺序排列的命名命令行参数吗?我浏览了文档但其中大部分内容都集中在根据提供的参数显示内容(例如--h).

现在,我的脚本读取有序的,未命名的参数:

myscript.py foo-val bar-val

使用sys.argv:

foo = sys.argv[1]
bar = sys.argv[2]
Run Code Online (Sandbox Code Playgroud)

但我想更改输入,以便通过命名参数与命令无关:

myscript.py --bar = bar-val --foo = foo-val

python arguments python-3.x

39
推荐指数
2
解决办法
2万
查看次数

如何在Linux上传递带有感叹号的参数?

我有一个简单的Python脚本,它接收用户名和密码作为参数,但我的密码包含两个感叹号.当我把我的剧本称为

salafek@dellboy:~/Desktop/$ emailsender.py -u username -p pass!!
Run Code Online (Sandbox Code Playgroud)

我之前输入的命令替换了感叹号:

salafek@dellboy:~/Desktop/$emailsender.py -u username -p "passemailsender.py -u username -p passwget wget http://www.crobot.com.hr/templog"
Run Code Online (Sandbox Code Playgroud)

我可以使用反斜杠(\)来逃避感叹号,但我的密码会更改.

有解决方案,如何在不更改密码的情况下逃避感叹号?

linux shell arguments

38
推荐指数
2
解决办法
3万
查看次数

有没有办法在可调用的方法中进行参数?

我已经创建了一段代码,它接受一个I​​P地址(来自另一个类中的main方法),然后循环遍历一系列IP地址,并在每个IP地址上执行.我有一个GUI前端,它正在崩溃(因此我为什么要完成多线程.我的问题是我不能再将IP地址作为我的ping代码中的参数作为其可调用的.我已经搜遍了所有为此,似乎无法找到解决这个问题的方法.有一种方法可以让一个可调用的方法来获取参数吗?如果没有,有没有其他方法可以实现我想要做的事情?

我的代码示例:

public class doPing implements Callable<String>{

public String call() throws Exception{

    String pingOutput = null;

    //gets IP address and places into new IP object
    InetAddress IPAddress = InetAddress.getByName(IPtoPing);
    //finds if IP is reachable or not. a timeout timer of 3000 milliseconds is set.
    //Results can vary depending on permissions so cmd method of doing this has also been added as backup
    boolean reachable = IPAddress.isReachable(1400);

    if (reachable){
          pingOutput = IPtoPing + " is reachable.\n";
    }else{
        //runs ping command once …
Run Code Online (Sandbox Code Playgroud)

java multithreading arguments ping callable

38
推荐指数
4
解决办法
6万
查看次数

可以部分应用不带关键字参数的函数的第二个参数吗?

以python内置pow()函数为例.

xs = [1,2,3,4,5,6,7,8]

from functools import partial

list(map(partial(pow,2),xs))

>>> [2, 4, 8, 16, 32, 128, 256]
Run Code Online (Sandbox Code Playgroud)

但是我如何将xs提高到2的幂?

要得到 [1, 4, 9, 16, 25, 49, 64]

list(map(partial(pow,y=2),xs))

TypeError: pow() takes no keyword arguments
Run Code Online (Sandbox Code Playgroud)

我知道列表理解会更容易.

python arguments partial-application

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

sam @mixin可以接受未定义数量的参数吗?

我正在尝试为过渡创建一个sass mixin.这就是我到目前为止所拥有的.

@mixin transition($var)
  -webkit-transition: $var
  transition: $var
Run Code Online (Sandbox Code Playgroud)

我希望能够像这样传递多个参数

@include transition(color .5s linear, width .5s linear)
Run Code Online (Sandbox Code Playgroud)

不幸的是,我收到以下错误

Syntax error: Mixin transition takes 1 argument but 2 were passed.
Run Code Online (Sandbox Code Playgroud)

有没有办法这样做,所以它在css中产生以下输出,同时仍然接受一个未定义数量的参数?

-webkit-transition: color .5s linear, width .5s linear;
transition: color .5s linear, width .5s linear;
Run Code Online (Sandbox Code Playgroud)

arguments sass undefined mixins

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

Python:在函数之间传递变量

我花了几个小时在这里和其他地方阅读,以及尝试,但我并不真正理解我确信这是一个非常基本的概念:在不同的函数之间传递值(作为变量).

例如,我将一大堆值分配给一个函数中的列表,然后希望稍后在另一个函数中使用该列表:

list = []

def defineAList():
    list = ['1','2','3']
    print "For checking purposes: in defineAList, list is",list
    return list

def useTheList(list):
    print "For checking purposes: in useTheList, list is",list

def main():
    defineAList()
    useTheList(list)

main()
Run Code Online (Sandbox Code Playgroud)

根据我对函数参数的理解,我希望这样做如下:

  1. 将'list'初始化为空列表; 打电话给主(这个,至少,我知道我说得对......)
  2. 在defineAList()中,将某些值分配给列表; 然后将新列表传递回main()
  3. 在main()中,调用useTheList(列表)
  4. 由于'list'包含在useTheList函数的参数中,我希望useTheList现在可以使用defineAList()定义的列表,而不是在调用main之前定义的空列表.

然而,这显然是错误的理解.我的输出是:

For checking purposes: in defineAList, list is ['1', '2', '3']
For checking purposes: in useTheList, list is []
Run Code Online (Sandbox Code Playgroud)

所以,既然"回归"显然没有做我认为它做的事情,或者至少它没有像我认为的那样去做......它实际上做了什么?你可以告诉我,使用这个例子,我需要做什么来从defineAList()获取列表并在useTheList()中使用它?当我看到它们发生时,我倾向于更好地理解事物,但是我所看到的很多适当的参数传递的例子也使用了我不熟悉的代码,并且在弄清楚发生了什么的过程中,我我真的没有理解这个概念.我正在使用2.7.

ETA-在过去,提出类似的问题,有人建议我使用全局变量而不仅仅是本地变量.如果它在这里也是相关的 - 为了我正在上课的目的,我们不允许使用全局变量.

谢谢!

python arguments function

36
推荐指数
2
解决办法
18万
查看次数

使用参数数组更改JavaScript函数的参数值无效

我正在学习JavaScript,我对arguments属性数组感到很困惑.

我有一个函数,它接受一个参数并返回它.当我传递参数并使用arguments[0] = value它重新分配时,它正在更新值.

function a(b) {
  arguments[0] = 2;
  return b;
}
console.log(a(1)); //returns 2
Run Code Online (Sandbox Code Playgroud)

但是当我调用没有参数的相同函数时它返回undefined.

function a(b) {
  arguments[0] = 2;
  return b;
}
console.log(a()); //returns undefined
Run Code Online (Sandbox Code Playgroud)

但即使我通过undefined,价值也会更新.

function a(b) {
  arguments[0] = 2;
  return b;
}
console.log(a(undefined)); //returns 2
Run Code Online (Sandbox Code Playgroud)

我认为如果你没有将参数传递给JavaScript函数,它会自动创建它并分配值,undefined并在更新后它应该反映更新的值,对吧?

a()a(undefined)是相同的东西,对不对?

javascript arrays arguments function variable-assignment

36
推荐指数
2
解决办法
2537
查看次数

Javascript函数和可选参数

我有两个几乎相同的javascript函数,用于启动jquery $ .get调用.函数的参数传递给被调用的脚本.

问题是一组调用需要另一个参数,而另一组则不需要.

为了实现这一点,我使用了我提到的两个几乎相同的javascript函数.他们来了:

function process(url, domid, domain, scan_id)
{
    $.get(url,
    {
        domain: domain,
        scan_id: scan_id
    },

    function(data)
    {
        $(domid).html(data);
    });
}

function process_type(url, domid, type, domain, scan_id)
{
    $.get(url,
    {
        domain: domain,
        type: type,
        scan_id: scan_id
    },

    function(data)
    {
        $(domid).html(data);
    });
}
Run Code Online (Sandbox Code Playgroud)

如您所见,第二个函数只接受一个名为'type'的附加参数,然后通过$ .get调用传递.

我想要结合这两个函数,但是我不知道如何可以选择包含第3个参数(数组/对象/它在{}中的任何内容(是的,javascript noob))在$ .get中传递.

编辑只是说....该死的,你们好.:d

javascript jquery arguments

35
推荐指数
3
解决办法
7万
查看次数

在TypeScript中将数组作为参数传递

我有两种方法:

static m1(...args: any[]) {
    //using args as array ...
}

static m2(str: string, ...args: any[]){
    //do something
    //....

    //call to m1
    m1(args);
}
Run Code Online (Sandbox Code Playgroud)

呼吁m1(1,2,3)按预期工作.但是,呼叫m2("abc",1,2,3)将转移到m1([1,2,3]),而不是预期:m1(1,2,3).

那么,如何args在调用m1in 时传递参数m2

arrays arguments typescript

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