Home > Software engineering >  Create class and represent it without quotation marks
Create class and represent it without quotation marks

Time:12-20

I need to represent my datatype PS without the leading and finishing quotation marks.
(so that ' ' becomes )

I tried overriding repr but cannot figure out, how to properly do it. My problem:

class E:                # Expression-Class
    pass

class AE(E):            # Arithmetic_Expression-Class
    pass

class BO(AE):           # Binary_Operation-Class
    pass

class P(BO):            # Plus-Class
    operator = PS()

class PS:               # Plus_Sign-Class
    def __repr__(self):
        return          # <- obviously raises an error 
                        # how to return ' ' string without the single quotes (so: ' ' ->  )?

CodePudding user response:

__repr__ has to return a string (str). If you return ' ' you are returning a string with just one plus sign in it. If you print() it, there will be no single quotes around it. The only reason you see single quotes is that whatever is printing it is not printing the value but the representation of the string .

>>> class PS:
...   def __repr__(self):
...     return ' '
...
>>> a = PS()
>>> a
 
>>> print(a)
 
>>> repr(a)
' '
  • Related