要确定课程,我可以这样做:
class A: pass
a = A
type(A) is type #True
Run Code Online (Sandbox Code Playgroud)
要么:
import inspect
inspect.isclass(A)
Run Code Online (Sandbox Code Playgroud)
但是如何确定类实例的类型,不知道类名?
像这样的东西:
isinstance(a, a.__class__.__name__)
#TypeError: isinstance() arg 2 must be a type or tuple of types
Run Code Online (Sandbox Code Playgroud)
我找到了一个解决方案,但它不适用于Python 3x
import types
class A: pass
a = A()
print(type(a) == types.InstanceType)
#AttributeError: 'module' object has no attribute 'InstanceType'
Run Code Online (Sandbox Code Playgroud)
解:
if '__dict__' in dir(a) and type(a) is not type:
Run Code Online (Sandbox Code Playgroud) 我正在尝试安装应用程序节点,但默认情况下我的环境是python 3,并且需要python 2.6.如何在FreeBSD中更改默认的python版本?
# cd /usr/local/bin
# ls -l | grep python
-r-xr-xr-x 2 root wheel 1246256 Jul 12 2011 python
-r-xr-xr-x 2 root wheel 1401 Jul 12 2011 python-config
-r-xr-xr-x 2 root wheel 6060 Jul 12 2011 python-shared
-r-xr-xr-x 2 root wheel 1408 Jul 12 2011 python-shared-config
-r-xr-xr-x 1 root wheel 3720 Jul 12 2011 python-shared2.6
-r-xr-xr-x 1 root wheel 1431 Jul 12 2011 python-shared2.6-config
-r-xr-xr-x 2 root wheel …Run Code Online (Sandbox Code Playgroud) 如何发出请求将一行中的所有字段分组?
表:
id | type
---------
1 | 1
1 | 1
2 | 2
3 | 3
3 | 3
Run Code Online (Sandbox Code Playgroud)
查询:
select concat('id(', count(type), ')') from T group by id;
Run Code Online (Sandbox Code Playgroud)
输出:
id(2)
id(1)
id(2)
Run Code Online (Sandbox Code Playgroud)
我想得到一个字符串: 'id(1)' = 2, 'id(2)' = 1, 'id(3)' = 2
如何在Python中自我调用匿名函数?
例如使用JavaScript:
标准方式:
function fn (a) {
if (a == 1) {
alert(a);
}
else {
alert(0);
}
/...
}
fn(1);
Run Code Online (Sandbox Code Playgroud)
自我调用匿名电话:
!function(a) {
if (a == 1) {
alert(a);
}
else {
alert(0);
}
/...
}(1);
Run Code Online (Sandbox Code Playgroud)
Python中有任何类似物吗?
我有代码:
#include <iostream>
using namespace std;
auto fn = ([](int x){
return [x](int y) {
return x * y;
};
});
int main() {
int i = fn(2)(4); // 8
cout << i << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常.但是,我想稍后再调用第二个函数:
auto i = fn(2);
i(4); //error: 'i' cannot be used as a function
Run Code Online (Sandbox Code Playgroud)
有没有办法稍后调用最后一个函数然后绑定第一个调用?