转换 python“类型”对象到字符串

发布于 2024-10-17 19:48:18 字数 181 浏览 1 评论 0原文

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

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

print("My type is " + type(some_object))  # (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(some_object))  # (which obviously doesn't work like this)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

╰つ倒转 2024-10-24 19:48:19

如果您想使用 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'
想你只要分分秒秒 2024-10-24 19:48:19

通过使用 str() 函数,您可以做到这一点。

 typeOfOneAsString=str(type(1))  # changes the type to a string type

By using the str() function you can do this.

 typeOfOneAsString=str(type(1))  # changes the type to a string type
情话墙 2024-10-24 19:48:18
print(type(some_object).__name__)

如果这不适合您,请使用以下

print(some_instance.__class__.__name__)

示例:

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

此外,在使用新式类与旧式类(即从 继承)时,type() 似乎存在差异对象)。对于新式类,type(someObject).__name__ 返回名称,对于旧式类,它返回instance

print(type(some_object).__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.

孤独患者 2024-10-24 19:48:18
>>> class A(object): pass

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

“转换成字符串”是什么意思?您可以定义自己的 __repr__ 和 __str__ 方法:

>>> 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 ;)

纵山崖 2024-10-24 19:48:18
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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文