识别一个变量是Python中的新式类? [英] Identifying that a variable is a new-style class in Python?

查看:114
本文介绍了识别一个变量是Python中的新式类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python 2.x,我想知道是否有一种方法来判断一个变量是一个新式的类吗?我知道如果它是一个老式的类,我可以做以下找到。

 导入类型

class oldclass:
pass

def test():
o = oldclass()
如果type(o)是types.InstanceType:
print'is old-style'
else:
print'is not old-style'

但我还没有找到任何适用于新式类的东西。我找到了这个问题,但是提出的解决方案似乎没有按预期工作,因为简单的值被标识为类。

  import inspect 

def newclass(object):
pass

def test():
n = newclass()
如果inspect.isclass(n):
print'Is class'
else:
print'Is NOT class'
if inspect.isclass(type(n)):
print'Is class'
else:
print'Is not class'
如果检查。 isclass(type(1)):
print'Is class'
else:
print'Is not class'
如果isinstance(n,object):
print 'is class'
else:
print'Is not class'
if isinstance(1,object):
print'Is class'
else:
print'is not class'

那么有什么办法吗?

解决方案

我认为你所要求的是: 我可以测试一个类是否在Python代码中定义为一个新式类?技术上简单的类型如 int 新式类,但仍然可以区分用Python编写的类和内置类型。



这是一些工作,虽然它有点黑客:

  def is_new_style(cls):
return hasattr(cls,'__class__')\
and \
('__dict__'in dir(cls)or hasattr(cls,'__slots__ '))


class new_style(object):
pass

class old_style():
pass

print is_new_style(int)
print is_new_style(new_style)
print is_new_style(old_style)


$ b b

Python 2.6的输出:

  False 
True
False



这是一个不同的方法:

  def is_new_style(cls):
return str(cls).startswith('< class')


I'm using Python 2.x and I'm wondering if there's a way to tell if a variable is a new-style class? I know that if it's an old-style class that I can do the following to find out.

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'

But I haven't been able to find anything that works for new-style classes. I found this question, but the proposed solutions don't seem to work as expected, because simple values as are identified as classes.

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, object):
    print 'Is class'
  else:
    print 'Is NOT class'

So is there anyway to do something like this? Or is everything in Python just a class and there's no way to get around that?

解决方案

I think what you are asking is: "Can I test if a class was defined in Python code as a new-style class?". Technically simple types such as int are new-style classes, but it is still possible to distinguish classes written in Python from the built-in types.

Here's something that works, although it's a bit of a hack:

def is_new_style(cls):
    return hasattr(cls, '__class__') \
           and \
           ('__dict__' in dir(cls) or hasattr(cls, '__slots__'))


class new_style(object):
    pass

class old_style():
    pass

print is_new_style(int)
print is_new_style(new_style)
print is_new_style(old_style)

Output from Python 2.6:

False
True
False

Here's a different way to do it:

def is_new_style(cls):
    return str(cls).startswith('<class ')

这篇关于识别一个变量是Python中的新式类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆