#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    145
    Rep Power
    10

    Class Concatenation


    Hi, my problem is probably best illustrated by way of an example, so here goes:
    [code]
    class myclass:
    def __init__ (self):
    self.x = 'hello'
    def __repr__ (self):
    return self.x


    test = myclass()
    test
    hello
    myvar = test + ' world!'
    Traceback (most recent call last):
    File "<pyshell#22>", line 1, in -toplevel-
    test + ' world'
    TypeError: unsupported operand type(s) for +: 'instance' and 'str'
    [code]
    I want to be able to overload the repr/str operators to allow concatenation of a class instance and a string.
    Thanks for all of your help.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Concatenation as in the string class is handled via the __add__(x, y) method. It should be simple enough to overload this method so that you return the new value from concatenating the value of the two instances .

    Code:
        def __add__(self, y):
            return self.x + y.x
    I hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    or another way would be if i understand what he/she wants:
    myvar = str(test) + ' world!'

    probably i dont:P
    Those people who think they know everything are a great annoyance to those of us who do.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by monkeyman23555
    or another way would be if i understand what he/she wants:
    myvar = str(test) + ' world!'

    probably i dont:P
    I don't think thats it monkey, here you're concatenating two strings outside of the class rather than adding the ability to concatenate two custom classes: I maybe wrong?

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    well i dont have any expirence with __repr__ never used it
    Those people who think they know everything are a great annoyance to those of us who do.
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Code:
    class myclass:
        def __init__(self):
            self.x = 'hello'
    
    myvar = '%s world!' % (myclass().x)
    myvar2 = myclass().x + ' world!'
    I modified your code a bit. In this case __repr__ isn't needed. It may be needed with whatever your actual code is, but I havn't seen your actual code.
    You can use either myvar or myvar2, they both do the same thing. Hope this helps.

    By the way, using % with strings can be much more effective than the + operator. Just adding one variable to a string constant it's okay to use the + operator, but when you get to including multiple strings and in certain parts of the string, it helps out a lot. %s is for strings. %d is for integers, and there are more data types you can use as well. These are just the two main ones.

    Code:
    >>> print '%d.fg/%s+fwgf<%s' % (56, 'hello', 'world')
    56.fg/hello+fwgf<world
    Last edited by †Yegg†; August 25th, 2005 at 05:47 PM.

IMN logo majestic logo threadwatch logo seochat tools logo