问题:将python’type’对象转换为字符串

我想知道如何使用python的反射功能将python’type’对象转换为字符串。

例如,我想打印一个对象的类型

print "My type is " + type(someObject) # (which obviously doesn't work like this)

I’m wondering how to convert a python ‘type’ object into a string using python’s reflective capabilities.

For example, I’d like to print the type of an object

print "My type is " + type(someObject) # (which obviously doesn't work like this)

回答 0

print type(someObject).__name__

如果那不适合您,请使用此:

print some_instance.__class__.__name__

例:

class A:
    pass
print type(A())
# prints <type 'instance'>
print A().__class__.__name__
# prints A

另外,type()使用新样式的类和旧样式的类(即从继承object)之间似乎也存在差异。对于新型类,type(someObject).__name__返回名称,对于旧样式的类,返回instance

print type(someObject).__name__

If that doesn’t suit you, use this:

print some_instance.__class__.__name__

Example:

class A:
    pass
print type(A())
# prints <type 'instance'>
print A().__class__.__name__
# prints A

Also, it seems there are differences with type() when using new-style classes vs old-style (that is, inheritance from object). For a new-style class, type(someObject).__name__ returns the name, and for old-style classes it returns instance.


回答 1

>>> class A(object): pass

>>> e = A()
>>> e
<__main__.A object at 0xb6d464ec>
>>> print type(e)
<class '__main__.A'>
>>> print type(e).__name__
A
>>> 

转换为字符串是什么意思?您可以定义自己的reprstr _方法:

>>> class A(object):
    def __repr__(self):
        return 'hei, i am A or B or whatever'

>>> e = A()
>>> e
hei, i am A or B or whatever
>>> str(e)
hei, i am A or B or whatever

还是我不知道..请添加说明;)

>>> class A(object): pass

>>> e = A()
>>> e
<__main__.A object at 0xb6d464ec>
>>> print type(e)
<class '__main__.A'>
>>> print type(e).__name__
A
>>> 

what do you mean by convert into a string? you can define your own repr and str_ methods:

>>> class A(object):
    def __repr__(self):
        return 'hei, i am A or B or whatever'

>>> e = A()
>>> e
hei, i am A or B or whatever
>>> str(e)
hei, i am A or B or whatever

or i dont know..please add explainations ;)


回答 2

print("My type is %s" % type(someObject)) # the type in python

要么…

print("My type is %s" % type(someObject).__name__) # the object's type (the class you defined)
print("My type is %s" % type(someObject)) # the type in python

or…

print("My type is %s" % type(someObject).__name__) # the object's type (the class you defined)

回答 3

使用str()

 typeOfOneAsString=str(type(1))

Using str()

 typeOfOneAsString=str(type(1))

回答 4

如果您想使用str()和自定义str方法。这也适用于代表。

class TypeProxy:
    def __init__(self, _type):
        self._type = _type

    def __call__(self, *args, **kwargs):
        return self._type(*args, **kwargs)

    def __str__(self):
        return self._type.__name__

    def __repr__(self):
        return "TypeProxy(%s)" % (repr(self._type),)

>>> str(TypeProxy(str))
'str'
>>> str(TypeProxy(type("")))
'str'

In case you want to use str() and a custom str method. This also works for repr.

class TypeProxy:
    def __init__(self, _type):
        self._type = _type

    def __call__(self, *args, **kwargs):
        return self._type(*args, **kwargs)

    def __str__(self):
        return self._type.__name__

    def __repr__(self):
        return "TypeProxy(%s)" % (repr(self._type),)

>>> str(TypeProxy(str))
'str'
>>> str(TypeProxy(type("")))
'str'

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。