相关疑难解决方法(0)

什么是Python中的"可调用"?

现在很清楚元类是什么,有一个相关的概念,我一直在使用,而不知道它的真正含义.

我想每个人都用括号做错了,导致"对象不可调用"异常.更重要的是,使用__init____new__导致想知道这种血腥__call__可以用于什么.

你能给我一些解释,包括魔术方法的例子吗?

python callable

286
推荐指数
8
解决办法
23万
查看次数

在python中模拟'local static'变量

请考虑以下代码:

def CalcSomething(a):
    if CalcSomething._cache.has_key(a):
      return CalcSomething._cache[a]
    CalcSomething._cache[a] = ReallyCalc(a)
    return CalcSomething._cache[a] 

CalcSomething._cache = { }
Run Code Online (Sandbox Code Playgroud)

这是我在python中模拟"局部静态"变量时最容易想到的方法.
让我困扰的是,CalcSomething._cache是函数的定义之外被提及,但另一种方法是类似的东西:

if not hasattr(CalcSomething, "_cache"):  
    setattr(CalcSomething, "_cache", { } )  
Run Code Online (Sandbox Code Playgroud)

在函数的定义中,这真的很麻烦.

有更优雅的方式吗?

[编辑]
只是为了澄清,这个问题不是关于本地函数缓存,正如上面的例子所暗示的那样.这是另一个简短的例子,其中'静态本地'可能很方便:

def ParseString(s):
    return ParseString._parser.parse(s)  
# Create a Parser object once, which will be used for all parsings.
# Assuming a Parser object is heave on resources, for the sake of this example.
ParseString._parser = Parser() 
Run Code Online (Sandbox Code Playgroud)

python

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

确定__getattr__是否为方法或属性调用

有没有办法使用__getattr__确定方法和属性调用之间的区别?

即:

class Bar(object):
    def __getattr__(self, name):
        if THIS_IS_A_METHOD_CALL:
            # Handle method call
            def method(**kwargs):
                return 'foo'
            return method
        else:
            # Handle attribute call
            return 'bar'

foo=Bar()
print(foo.test_method()) # foo
print(foo.test_attribute) # bar
Run Code Online (Sandbox Code Playgroud)

这些方法不是本地的,因此无法使用getattr/callable来确定它.我也理解方法是属性,并且可能没有解决方案.只是希望有一个.

python getattr

8
推荐指数
1
解决办法
1449
查看次数

如何使用“预期条件”来检查 python-selenium 中的元素?

我无法理解如何使用“预期条件”来检查元素是否存在。鉴于此文档,根本不清楚如何使用它。我试过下面的代码

 def _kernel_is_idle(self):
    return EC.visibility_of_element_located((By.XPATH, '//*[@id="kernel_indicator_icon" and @title="Kernel Idle"]'))
Run Code Online (Sandbox Code Playgroud)

以检查元素(可作为类中的方法调用)的想法。有两件事没有任何意义:

  1. 根据文档(我必须查找源代码!),此方法应返回TrueFalse。但是,它返回以下内容:

    <selenium.webdriver.support.expected_conditions.visibility_of_element_located object at 0x110321b90>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果没有 ,此功能如何工作webdriver?通常你总是有这样的电话

    driver.do_something()
    
    Run Code Online (Sandbox Code Playgroud)

但是对于“预期条件”,webdriver 的参考在哪里?

python selenium

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

Keras 函数式 API 的语法

我对 keras 函数式 API 中的语法如何工作有点困惑。它对于定义复杂的多输入和输出模型非常有用。但语法对我来说有点令人困惑。

new_layer = Conv2d(...)(old_layer)
Run Code Online (Sandbox Code Playgroud)

据我所知 Conv2d 是一个Conv2d()() 语法在python中如何工作?

api syntax functional-programming keras

5
推荐指数
2
解决办法
821
查看次数

编程语言/平台,具有对AST的运行时访问

我正在寻求为一个简短的演示实现一些概念验证演示,其中正在运行的代码知道当前正在执行的代码块的散列"值".例如:

function BBB(a) {
  a = 2 * a;
  print me.hash;          --> "xxxxxxx" (value of BBB-syntax represenation)
  return a;                              
}

function AAA(a, b, c) {
  d = BBB(a);
  print me.hash;          --> "yyyyyyy" (value of AAA-Syntax representation, possibly dependant on value of BBB, but not necessary)
  return d;
}
Run Code Online (Sandbox Code Playgroud)

我本能地转向LISPish语言,但还没有成功使用Scheme.而且我很长时间没有接触过Common LISP,我怀疑它可能会这样做(提示赞赏).它不一定非常快,或者一个受欢迎的平台,可以是最具学术性和最奇怪的平台.这只是一个演示.

有没有人知道一种语言/平台能够开箱即用或者修补相对较少?我更喜欢某种解析/树状的东西,而不是实际的源代码.

programming-languages functional-programming metaprogramming common-lisp

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

Python-类可调用是什么意思?

我试图理解Python中的“可调用对象”是什么,以及类可调用的含义。我在玩以下代码:

class A(object):

    def __init__(self):
        pass

print("Is A callable? " + str(callable(A)))
a=A()
print("created a")
a()
Run Code Online (Sandbox Code Playgroud)

得到以下结果:

Is A callable? True
created a
Traceback (most recent call last):    
File "test2.py", line 11, in <module>
a()
TypeError: 'A' object is not callable  
Run Code Online (Sandbox Code Playgroud)

此外,

print(type(A.__call__()))
Run Code Online (Sandbox Code Playgroud)

给出:

<class '__main__.A'>
Run Code Online (Sandbox Code Playgroud)

这是否意味着A类具有__call__方法?为什么是class类?

A.__call__()每次使用A()实例化时都会被调用吗?

python callable

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

为什么这是类型错误而不是语法错误

代码为什么会这样

print("Average =" (sum/count))
Run Code Online (Sandbox Code Playgroud)

产生类型错误而不是语法错误,看到逗号丢失了吗?

谢谢.

python syntax typeerror

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