背景:我试图理解为什么静态和类方法在作为描述符时不可调用,而类的普通方法(即既不是静态也不是类方法的类方法)和不是类属性的函数都是描述符并可调用。
在Python中,可调用类型的定义是什么?
来自https://docs.python.org/3/reference/datamodel.html
可调用类型这些是可以应用函数调用操作(请参阅“调用”部分)的类型:
运算符是“函数调用操作”吗()?那么可调用类型是否定义为其实例()可以应用函数调用运算符的类型呢?
来自https://docs.python.org/3/reference/expressions.html#calls
主对象必须计算为可调用对象(用户定义函数、内置函数、内置对象的方法、类对象、类实例的方法以及所有具有方法的对象
__call__()都是可调用的)。
这是否意味着可调用类型可能有也可能没有
__call__()方法?如果一个类有__call__()方法,那么它一定是可调用类型?如果一个类没有__call__()
方法,那么它可能是也可能不是可调用类型?
难道“用户定义函数、内置函数、内置对象的方法、类对象、类实例的方法”就没有方法吗
__call__()?它们是可调用类型的实例吗?它们分别有哪些具体类型?
谢谢。
我不知道如何使用python3.2的这个新属性。在那里,可以使用可调用的类,而不是实现logging.Filter类。
好的代码在这里
class ignore_progress(logging.Filter):
def filter(self, record):
return not ('Progress' in record.getMessage())
class log_progress(logging.Filter):
def filter(self, record):
return ('Progress' in record.getMessage())
def contain_progress(record):
return not ('Progress' in record.message)
logging_dict = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"standard": {
"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s",
}
},
"filters": {
"ignore_progress": {
'()': ignore_progress,
}
},
"handlers": {
"default": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"formatter": "standard",
},
"file": { …Run Code Online (Sandbox Code Playgroud) 假设我有一个可调用存储为变量:
$callable = function($foo = 'bar', $baz = ...) { return...; }
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到“酒吧”?
if (is_callable($callable)) {
return func_get_args();
}
Run Code Online (Sandbox Code Playgroud)
不幸的func_get_args()是,对于当前函数,是否可以获得参数的键值对?
嗨,我有 2 列的数据框:
+----------------------------------------+----------+
| Text | Key_word |
+----------------------------------------+----------+
| First random text tree cheese cat | tree |
| Second random text apple pie three | text |
| Third random text burger food brain | brain |
| Fourth random text nothing thing chips | random |
+----------------------------------------+----------+
Run Code Online (Sandbox Code Playgroud)
我想生成第 3 列,其中一个单词出现在文本中的 key_word 之前。
+----------------------------------------+----------+-------------------+--+
| Text | Key_word | word_bef_key_word | |
+----------------------------------------+----------+-------------------+--+
| First random text tree cheese cat | tree | text | |
| …Run Code Online (Sandbox Code Playgroud) 在 Clojure 中,哈希映射和向量实现了invoke,因此它们可以用作函数,例如
(let [dict {:species "Ursus horribilis"
:ornery :true
:diet "You"}]
(dict :diet))
lein> "You"
Run Code Online (Sandbox Code Playgroud)
或者,对于向量,
(let [v [42 613 28]]
(v 1))
lein> 613
Run Code Online (Sandbox Code Playgroud)
可以通过让它们实现 Clojure 中的可调用对象IFn。我是 Common Lisp 的新手——可调用对象是否可能,如果是的话,实现它会涉及什么?我真的很想能够做这样的事情
(let ((A (make-array (list n n) ...)))
(loop for i from 0 to n
for j from 0 to m
do (setf (A i j) (something i j)))
A)
Run Code Online (Sandbox Code Playgroud)
而不是让代码乱七八糟aref。同样,如果您可以以相同的方式访问其他数据结构的条目,例如字典,那将会很酷。
我已经查看了Lisp/Scheme 中函数对象的wiki 条目,似乎拥有一个单独的函数命名空间会使 CL 的问题复杂化,而在 Scheme 中,您可以使用闭包来做到这一点。
本质上,我试图将参数传递给函数,但推迟执行该函数直到以后.我不想拖延一段时间或者我只想sleep.这是我要用的用法
import requests
def test_for_active_server(server_address):
response = requests.get(server_address)
try_n_times(func, expected_error, n_iterations):
for i in range(n_iterations):
try:
func()
break
except expected_error:
continue
else:
return False
return True
try_n_times(
create_callable(test_for_active_server("http://localhost/"),
requests.ConnectionError, 10)
Run Code Online (Sandbox Code Playgroud)
这里的问题当然是,当我打电话时,test_for_active_server("http://localhost/")它会立即运行,所以这个论点create_callable就是None.我相信我可以用类似的东西做到这一点
def create_callable(func, func_args: List[str], func_kwargs: Dict[str, str], *args):
def func_runner():
func(*args, *func_args, **func_kwargs)
return func_runner
Run Code Online (Sandbox Code Playgroud)
然后用它作为
create_callable(test_for_active_server, "http://localhost")
Run Code Online (Sandbox Code Playgroud)
但这很尴尬.有一个更好的方法吗?