小编Dav*_*sen的帖子

在Python中确定变量是一个新式的类?

我正在使用Python 2.x,我想知道是否有办法判断一个变量是否是一个新式的类?我知道,如果它是一个旧式的课程,我可以做以下事情来找出答案.

import types

class oldclass:
  pass

def test():
  o = oldclass()
  if type(o) is types.InstanceType:
    print 'Is old-style'
  else:
    print 'Is NOT old-style'
Run Code Online (Sandbox Code Playgroud)

但我找不到任何适用于新式课程的东西.我发现了这个问题,但提出的解决方案似乎没有按预期工作,因为简单的值被识别为类.

import inspect

def newclass(object):
  pass

def test():
  n = newclass()
  if inspect.isclass(n):
    print 'Is class'
  else:
    print 'Is NOT class'
  if inspect.isclass(type(n)):
    print 'Is class'
  else:
    print 'Is NOT class'
  if inspect.isclass(type(1)):
    print 'Is class'
  else:
    print 'Is NOT class'
  if isinstance(n, object):
    print 'Is class'
  else:
    print 'Is NOT class'
  if isinstance(1, …
Run Code Online (Sandbox Code Playgroud)

python class python-2.x

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

从库中退出()可以替换为抛出异常吗?

我正在使用C++的C/Fortran库,并且库调用exit().我希望它抛出异常,以便调用我的C++程序中的析构函数.我已经能够创建一个抛出异常的exit定义,但是仍然会调用terminate.是否有防止终止被调用并允许正常的异常处理发生?

更新:在评论中指出,这适用于x64但在x86上失败,因此主要问题是"有没有办法使x86像x64一样工作?".

更新2:请参阅我的回答,了解为什么这不适用于x86以及如何解决它.

这是我的测试代码:

test_exception.c

#include <stdlib.h>

void call_c() { exit(1); }
Run Code Online (Sandbox Code Playgroud)

test_exception.cpp

#include <iostream>
#include <stdexcept>

extern "C" void call_c();

extern "C" void exit(int value)
{
  throw std::runtime_error(std::string("Throwing an exception: ") + char('0' + value));
}

int main()
{
  try {
    call_c();
  } catch (const std::exception &e) {
    std::cerr << e.what() << std::endl;
    return 1;
  }

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

使用以下命令构建:

gcc -c test_exception.c -o test_exception_c.o
g++ -c test_exception.cpp -o test_exception_cpp.o
g++ test_exception_c.o test_exception_cpp.o -o test_exception
Run Code Online (Sandbox Code Playgroud)

c c++ exception

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

获取PostgreSQL中视图/表所依赖的表的列表

在PostgreSQL中,有没有办法根据外键的使用和对给定表的访问来获取视图/表所依赖的所有表?

基本上,我希望能够使用脚本复制视图/表的结构,并希望能够自动获取我还需要复制的表列表,以便一切仍然正常工作.

这种反应似乎朝着正确的方向发展,但并没有给出我期望/需要的结果.有什么建议?

postgresql

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

标签 统计

c ×1

c++ ×1

class ×1

exception ×1

postgresql ×1

python ×1

python-2.x ×1