您可以将对象类的实例设置为字符串输入吗
您可以将对象类返回作为字符串吗?
我尝试使用__ str __
和__ epr __
方法来设置对象类。
class A(object):
def __str__(self):
return "the string"
def __repr__(self):
return "the repr"
a = A()
这适用于打印语句,并直接运行类实例。
print a
a
the string
# Result: the repr #
但是,当尝试将类实例设置为字符串输入时,我会得到此错误。
from PySide2 import QtWidgets
QtWidgets.QLabel().setText(a)
# Error: 'PySide2.QtWidgets.QLabel.setText' called with wrong argument types:
# PySide2.QtWidgets.QLabel.setText(A)
# Supported signatures:
# PySide2.QtWidgets.QLabel.setText(unicode)
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# TypeError: 'PySide2.QtWidgets.QLabel.setText' called with wrong argument types:
# PySide2.QtWidgets.QLabel.setText(A)
# Supported signatures:
# PySide2.QtWidgets.QLabel.setText(unicode) #
我是否需要覆盖我需要的结果?
更新
我仅限于使用Python 2.7,因为我正在编写不使用Python 3的Maya版本的工具,
我想避免包装对象类实例:
# Undesired syntax
QtWidgets.QLabel().setText(str(a))
QtWidgets.QLabel().setText(unicode(a))
# Desired syntax
QtWidgets.QLabel().setText(a)
我尝试使用字符串类,该类别在解析实例时确实返回字符串在课堂上,但是您无法更改该类首先使用的字符串。 例如:
class A(str):
def __str__(self):
return "the string"
def __repr__(self):
return "the repr"
a = A("string")
QtWidgets.QLabel().setText(a)
这将把文本设置为“ String”
,但是到目前为止,我在初始化后无法更改此字符串。我尝试了self .__ str __。im_self(“ new String”)
之类的事情,但它永远不会更改存储的变量类数据。
我相信这是我在对象类中遇到的核心问题,我无法更改从存储的实例中返回的内容。
在这里,我发现将课程倾倒到JSON文件时讨论相同的问题,使用Unicode类:
https://stackoverflow.com /a/13489906/3950707
我觉得我需要找到任何核心方法或属性在解析Instanced类时返回,并在此处修改STR/UNICODE数据,因为这返回了字符串,但也充当对象类。
您可以通过覆盖__新的__
方法来更改分析类实例时返回的内容,但是我在以这种方式实现所需的结果而失败了,因为这仅在首先初始化的类时运行。
def __new__(cls, *args, **kwargs):
#return str(object.__new__(cls))
return str(super(A, cls).__new__(cls))
Can you set the object class return as a string?
I have tried setting the object class with the __str__
and __repr__
methods overwritten.
class A(object):
def __str__(self):
return "the string"
def __repr__(self):
return "the repr"
a = A()
This works for print statements, and running the class instance directly.
print a
a
the string
# Result: the repr #
However, when trying to set the class instance as a string input, I get this error.
from PySide2 import QtWidgets
QtWidgets.QLabel().setText(a)
# Error: 'PySide2.QtWidgets.QLabel.setText' called with wrong argument types:
# PySide2.QtWidgets.QLabel.setText(A)
# Supported signatures:
# PySide2.QtWidgets.QLabel.setText(unicode)
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# TypeError: 'PySide2.QtWidgets.QLabel.setText' called with wrong argument types:
# PySide2.QtWidgets.QLabel.setText(A)
# Supported signatures:
# PySide2.QtWidgets.QLabel.setText(unicode) #
Is there another method I need to overwrite to get the results I need?
Update
I am limited to using python 2.7 as I am writing tools for versions of Maya that do not use Python 3
I want to avoid wrapping the object class instance:
# Undesired syntax
QtWidgets.QLabel().setText(str(a))
QtWidgets.QLabel().setText(unicode(a))
# Desired syntax
QtWidgets.QLabel().setText(a)
I have tried using a string class, which does return a string when parsing the instance of the class, but you are unable to change the string that the class was first instanced with.
eg:
class A(str):
def __str__(self):
return "the string"
def __repr__(self):
return "the repr"
a = A("string")
QtWidgets.QLabel().setText(a)
This will set the text as "string"
, however I have so far been unable to change this string after initialisation. I have tried things like self.__str__.im_self("new string")
, but it never changes the stored variable class data.
I believe this is the same core problem I'm having with the object class, where I am unable to change what is being returned from the stored instance.
Here I found @seeg discussing the same problem when dumping the class to a JSON file, using a Unicode class:
https://stackoverflow.com/a/13489906/3950707
I feel like I need to find whatever core method or attribute is being returned when parsing the instanced class, and amend the str/unicode data there, as this returns a string, but also acts as an object class.
You can change what is returned when parsing the class instance by overwriting the __new__
method, however I have been unsuccessful in achieving my desired results this way, as this only runs when the class is first initialised.
def __new__(cls, *args, **kwargs):
#return str(object.__new__(cls))
return str(super(A, cls).__new__(cls))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论