0

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))
Adam Sirrelle
  • 357
  • 8
  • 18
  • As the error clearly says, you're expected to use an `unicode` type: `return u'whatever'`. That's another reason for which you shouldn't be using Python 2 anymore. – musicamante Jul 08 '22 at 04:56
  • QtWidgets.QLabel().setText(str(a)) – DarkKnight Jul 08 '22 at 04:57
  • @musicamante, I've tried returning the string as u"", and unicode(""), this doesn't seem to be the issue. – Adam Sirrelle Jul 08 '22 at 04:59
  • @Stuart, I want to avoid wrapping the class in str(a), or unicode(a), if possible. – Adam Sirrelle Jul 08 '22 at 05:00
  • @AdamSirrelle unfortunately, you're using an obsolete version of Python that is known to have serious issues with unicode. That's one of the reasons for which the migration to Python 3 was so important. – musicamante Jul 08 '22 at 05:03
  • @musicamante, I'm currently bound to python 2.7 due to the version of Maya I'm using. I think only the most recent version of Maya uses python 3, and I won't be moving onto that for a while. – Adam Sirrelle Jul 08 '22 at 05:06
  • @AdamSirrelle then you are stuck with workarounds. There's no other option. Python 2 was officially obsolete 2 years ago, and the warning about that date came way before that date. I completely understand your requirement and feel your annoyances about that, but that's it: it isn't supported anymore. Not only, you're using an internal library, not a shared one, so there's no even space for "fixing" it. If you have to keep using it, you must use work arounds: if `u'...'` doesn't suffice, you *must* use `str(...)`. – musicamante Jul 08 '22 at 05:11
  • @AdamSirrelle The *setText()* function clearly expects object references of a certain type (Unicode strings by the looks of it). It's equally obvious that it doesn't attempt to call either *str()* or *repr()* on objects that it doesn't recognise. Therefore you have no option (that I can think of) but to do it yourself – DarkKnight Jul 08 '22 at 05:12
  • @alexpdev, I've tried a str class, but this introduces a new problem where the string the class was initialised with cannot be changed. – Adam Sirrelle Jul 08 '22 at 05:18
  • @AdamSirrelle please [edit] your post showing those attempts. – musicamante Jul 08 '22 at 05:37

0 Answers0