4

Returning self in f1 gives me <__main__.Test instance at 0x11ae48d40>. I would like to be able to return 'apples and cinnamon' but I can't do str(self). Is there a way for me to do this?

class Test:
    def __init__(self, thing):
        self.thing = thing
    def f1(self, thing):
        return self + " and " + thing #<<<

a = Test("apples")
a.f1("cinnamon")
3
  • 1
    Why do you pass "cinnamon" to f1 when you don't actually use it? Commented Nov 28, 2013 at 20:51
  • I want to be able to return a string like: Apples and cinnamon by doing return self + " and " + thing, but I can't. Commented Nov 28, 2013 at 20:55
  • 1
    apples is not the string representation of self. Did you want to return self.thing instead? Commented Nov 28, 2013 at 20:55

3 Answers 3

5

To 'pretty print' the object itself, define __str__ like so:

class Test(object):
    def __init__(self, thing):
        self.thing = thing

    def __str__(self):
        return self.thing

 >>> a=Test('apple')
 >>> print a
 apple

If you want the representation to be custom, add __repr__:

class Test(object):
    def __init__(self, thing):
        self.thing = thing
    def __repr__(self):
        return self.thing 

>>> Test('pear')
pear

If you want to create a string as stated in your edit, you can do this:

class Test(object):
    def __init__(self, thing):
        self.thing = thing

    def andthis(self, other):
        return '{} and {}'.format(self.thing, other)

>>> apple=Test('apple')
>>> apple.andthis('cinnamon')
'apple and cinnamon'
>>> Test('apple').andthis('carrots')
'apple and carrots' 
Sign up to request clarification or add additional context in comments.

2 Comments

Nope, it will still output <main.Test instance ...> when calling f1(). Try overriding _repr_
That would still print <__main__.Test instance at 0xhexaddress>; __str__ is only used when calling str() on an object (which is what print does).
1

you should add

def __str__(self):
    return self.thing

so it looks like this

class Test:
    def __init__(self, thing):
        self.thing = thing

    def f1(self, thing):
        return str(self) + " and " + thing

    def __str__(self):
        return self.thing

a = Test("apples")
print a
>> "apples"
print a.f1("orange")
>> "apples and orange"

Comments

0

If you wanted f1() to return a string, then do so:

def f1(self, otherthing):
    return '{} and {}'.format(self.thing, otherthing)

Here we use str.format() to put self.thing and otherthing together into a new string, which is returned. Note that you need to explicitly refer to self.thing here.

You could also use string concatenation, like in your own code:

def f1(self, otherthing):
    return self.thing + ' and ' + otherthing

but again, you need to refer to self.thing explicitly.

Demo:

>>> class Test:
...     def __init__(self, thing):
...         self.thing = thing
...     def f1(self, otherthing):
...         return '{} and {}'.format(self.thing, otherthing)
... 
>>> a = Test("apples")
>>> a.f1("cinnamon")
'apples and cinnamon'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.