标签: callable

如何从Callable()返回对象

我试图从call()返回一个二维数组,我遇到了一些问题.到目前为止我的代码是:

//this is the end of main   
Thread t1 = new Thread(new ArrayMultiplication(Array1, Array2, length));
t1.start(); 
}

    public int[][] call(int[][] answer)
    {       

    int[][] answer = new int[length][length]; 

    answer = multiplyArray(Array1, Array2, length); //off to another function which returns the answer to here  

    return answer;                                  
    }
Run Code Online (Sandbox Code Playgroud)

这段代码编译,这不是返回我的数组.我确定我可能使用了错误的语法,但我找不到任何好的例子.

编辑:改了一下

java multithreading callable

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

使用sum()函数时,为什么'int'对象不可出错?

我想弄清楚为什么我在一个范围上使用sum函数时会出现错误.

这是代码:

data1 = range(0, 1000, 3)
data2 = range(0, 1000, 5)
data3 = list(set(data1 + data2)) # makes new list without duplicates
total = sum(data3) # calculate sum of data3 list's elements
print total
Run Code Online (Sandbox Code Playgroud)

这是错误:

line 8, in <module> total2 = sum(data3)
TypeError: 'int' object is not callable
Run Code Online (Sandbox Code Playgroud)

我找到了错误的解释:

在Python中,"可调用"通常是一个函数.该消息意味着您正在处理一个数字(>"int"),就好像它是一个函数("可调用"),因此Python不知道该怎么做,所以它>停止.

我还读到sum()能够在列表中使用,所以我想知道这里出了什么问题?

我刚刚在IDLE模块中尝试过,它工作正常.但是,它在python解释器中不起作用.关于如何做的任何想法?

python sum callable python-2.7

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

在Callable中处理Thread.interrupted()的正确方法?

在Callable中处理Thread.interrupted()的正确方法是什么?我猜测callable应该抛出InterruptedException; 例如:

public class MyCallable implements Callable<Object> {
    public Object call() {
        Object result = null;

        // Simulate long-running operation that calculates result
        while (true) {
            ...
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }
        }

        result = ... // something produced by the long-running operation    

        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是正确的,还是有更合适的方法来处理它?谢谢.

java multithreading future callable

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

使用callable(x)vs.hasattr(x,"__ call__")

我正在编写针对3.2及更高版本的Python.看起来使用内置函数callable是最简单有效的方法.我见过的建议hasattr(x, "__call__"),collections.Callable(x)以及只使用try/except周围尝试的呼叫.

我已经测试了可调用的项目(类和函数),使用timeit100,000次迭代; 在两种情况下,使用callable只需要检查属性的大约75%的时间.当项目不可调用(整数和字符串)时,使用与类或函数相同的可调用停留时,检查属性的价格比类或函数贵2.3倍.我没想到会有这种差异,但它也倾向于采用简洁明了的callable(x)方法.

但我相对较新的Python而且没有专家,所以我不知道我应该使用hasattr方法或其他方法吗?

FWIW,各种时间的结果如下.第一个字符只是t表示timeit,第二个字符表示被测对象的类型(c = class,f = function,i = integer,s = string),其余表示方法(attr-check属性, call - use callable,try - use try/except).

tcattr 0.03665385400199739
tccall 0.026238360142997408
tctry 0.09736267629614304
tfattr 0.03624538065832894
tfcall 0.026362861895904643
tftry 0.032501874250556284
tiattr 0.08297350149314298
ticall 0.025826044152381655
titry 0.10657657453430147
tsattr 0.0840187013927789
tscall 0.02585409547373274
tstry 0.10742772077628615

python callable python-3.x

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

'int'对象在python中不是可调用的错误

我收到这个错误:

Traceback (most recent call last):
  File "C:\Users\George\Desktop\ex3.py", line 15, in <module>
    s=s+d*2(-1/6.)*(u-1)*(u-2)*(u+2)*(u-4)
TypeError: 'int' object is not callable
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

x=input()
z=input()
n=input()
while x>=z:
    x=input()
    z=input()
while n<0:
    n=input()
while n>0:
    d=(z-x)/1.*n
    k=1
    s=(d/2.)*((-1/6.)*(x-1)*(x-2)*(x+2)*(x-4)+(-1/6.)*(z-1)*(z-2)*(z+2)*(z-4))
    while k<=n-1:
        u=x+k*d
        s=s+d*2(-1/6.)*(u-1)*(u-2)*(u+2)*(u-4)
        k=k+1
        print "%.3f" %s
        x=input()
        z=input()
        n=input()
        if n>0:
            while x>=z:
                x=input()
                z=input()
Run Code Online (Sandbox Code Playgroud)

python int callable

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

Python 3:使str对象可调用

我有一个Python程序,需要用户输入.我存储用户输入一个名为"userInput"的字符串变量.我希望能够调用用户输入的字符串...

userInput = input("Enter a command: ")
userInput()
Run Code Online (Sandbox Code Playgroud)

从这里,我得到错误:TypeError:'str'对象不可调用

目前,我有程序做这样的事情:

userInput = input("Enter a command: ")
if userInput == 'example_command':
    example_command()

def example_command():
     print('Hello World!')
Run Code Online (Sandbox Code Playgroud)

显然,这不是处理大量命令的非常有效的方法.我想让str obj可以调用 - 无论如何这样做?

python string object callable

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

Java - 在ExecutorCompletionService中定义Callable的超时

我使用ExecutorCompletionService遇到了以下问题.我想在不同的线程中调用很多Callable.这些Callable不会彼此共享任何信息.我需要为每个Callable定义一个超时,例如.运行时间不要超过5秒.每个Callable都可以在启动时不知道的不同时间运行.在超时之后,线程应该被停止/杀死,结果对我来说不再有趣.不应该影响其他"正常"运行的线程.

因此,让我们以简单的可调用和我当前的Java代码为例.

import java.util.Date;
import java.util.concurrent.Callable;

public class Job implements Callable<Integer> {

    int returnValue = 0;
    long millis = 0;

    public Job(long millis, int value) {
        this.millis = millis;
        this.returnValue = value;
    }

    @Override
    public Integer call() throws Exception, InterruptedException {
        try {
            System.out.println(new Date() + " " + returnValue + " started");
            Thread.sleep(millis);
            System.out.println(new Date() + " " + returnValue + " finished");
            return returnValue;
        } catch (InterruptedException e) {
            System.out.println(new Date() + " " + returnValue …
Run Code Online (Sandbox Code Playgroud)

java multithreading timeout callable executorservice

7
推荐指数
2
解决办法
6400
查看次数

如何制作"可调用功能"

我目前正在编写一个名为f_from_data的python定义,它在一行上使用了插值查找点到目前为止我写的这个:

def f_from_data(xs, ys, x):
    xfine = np.linspace(min(xs), max(xs), 10000)
    y0 = inter.interp1d(xs, ys, kind = 'linear')
    ans = (y0(xfine))[numpy.searchsorted(xfine, x)]
    ans =  round(ans,2)
    return ans
Run Code Online (Sandbox Code Playgroud)

这给了我想要的东西,所以我可以输入:

f = f_from_data([3, 4, 6], [0, 1, 2])
print f(3)
>>>0.0
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?我环顾四周但似乎找不到任何东西,因为我觉得它真的很微不足道,但我只是想念一些东西.

python numpy callable

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

在哪里使用可调用以及在哪里使用Runnable接口?

我是Java的新手,我正在阅读多线程的概念,在进行多线程的各种实现时,我经历了这两个概念.这在Java问题中Runnable和Callable接口之间的区别指出了两者之间的区别以及使用的位置.

我怀疑Callable是否能够完成Runnable的所有功能,为什么这么多人使用Runnable而不是可调用?与Runnable Inteface相比,实现Callable接口是否有额外的开销?

java callable runnable

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

在PHP中,将lambda表达式用于可调用而不是字符串(或数组)是否更有效?

在PHP中,某些函数将"可调用"作为参数,这意味着您可以指定要在某一点执行的函数.一个例子是array_map.

PHP允许您以多种方式指定可调用对象,例如:

// as a string:
$lowerCaseStrings = array_map('strtolower', $arrayOfStrings);

// object methods as an array
// (this could be done with DateTime directly, of course):
class DateFactory {
    private $format;

    public function __construct($format) {
        $this->format = $format;
    }

    public function newDate($dateString) {
        return DateTime::createFromFormat($this->format, $dateString); 
    }
}

$factory = new DateFactory('Y-m-d');
$dates = array_map(array($factory, 'newDate'), $arrayOfDateStrings);

// as a lambda expression / closure:
$dates = array_map(
        function ($dateString) {
            return DateTime::createFromFormat('Y-m-d', $dateString);
        },
        $arrayOfDateStrings
    ); …
Run Code Online (Sandbox Code Playgroud)

php arrays lambda callable

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