[Tutor] How to write the __str__ function
Martin A. Brown
martin at linux-ip.net
Sun May 14 14:59:13 EDT 2017
Hello and greetings,
> I need some advice that I have been embarrased to ask for, because
> I think that my error is so elementary.
Well, there are two benefits to trying to write down questions like
this when you encounter them.
1) Rubber Duck debugging (if you have not heard of it); sometimes
the act of describing the problem / question is enough for you
to figure it out in the process
2) If you don't quite get there, then you have a description that
somebody can easily review and respond to.
> I have written, as advised by the tutors, a complex program in a
> topic that interests me. The program works well and the tests are
> OK.
Right on!
> Now I want to add a __str__ function, which I thought would be
> straightforward. But I cannot get it right.
>
> The code that I have so far is as folows:
>
> def __str__(self):
> return("\n"
> " Output from __str__ of POCWP. "
> "\n"
> "\n After the first turnover, during the "
> "'Population Of Capitals Init' cycle,"
> "\n the productivities were raised from 1.0 "
> "\n to a specific Unit Constant Capital (UCC) "
> "for each specific capital: "
> "\n The input value for the mean of UCC "
> "was %7.5f" % (self.ucc),
> "\n The fractional sigma (FractionalSTD)"
> " of UCC that was input was %7.5f " % (self.fractsigma_ucc))
>
> The error message is:
>
> TypeError: __str__ returned non-string (type tuple)
>
> When I omit either or both of the objects; self.ucc or
> self.fractsigma_ucc, the code works fine.
>
> So, I guess my code has an error. But I cannot detect the error.
It is not the omission of the objects, but rather the comma which is
posing you this puzzle.
> Guidance would be much appreciated.
So, the error message tells you that you are creating a tuple, so
let's look at why / how you are creating a tuple. I'm going to
remove your variable names and use x and y.
x, y = 5, 7
thing = ("var x=%s" % (x,), "var y=%s" % (y,)) # -- your code
type(thing)
# you will see: <class 'tuple'>
But, this is what you are doing when you remove one of the
variables:
thing = ("var x=unknown var y=%s" % (y,))
type(thing)
# you will see: <class 'str'>
What's going on here?
thing = ("no comma here")
type(thing)
# you will see: <class 'str'>
thing = ("no comma here",)
type(thing)
# you will see: <class 'tuple'>
So, when you are using parentheses, you can create a tuple, but you
must include a comma, otherwise, you are not actually creating a
tuple. Of course, you wish to create a string, not a tuple, so you
want to bear in mind that you are creating a tuple when you include
the comma in the line that ends with: (self.ucc),
I have, therefore, a few small suggestions:
1. Put all of the variables replacements at the end.
thing = ("var x=%s\nvar y=%s" % (x,y))
2. When creating the replacements, also use tuples (see next
point, too):
"was %7.5f" % (self.ucc,)
3. Use separate statements for creating and returning the string.
See below where I'm using triple-quoted strings for easier
editing [0].)
Good luck with Python,
-Martin
===================
def thing():
x, y = 3.141592653589793, 2.718281828459045
text = '''\n
Output from __str__ of POCWP.
After the first turnover, during the 'Population Of Capitals Init' cycle,
the productivities were raised from 1.0
to a specific Unit Constant Capital (UCC) for each specific capital:
The input value for the mean of UCC was %7.5f
The fractional sigma (FractionalSTD) of UCC that was input was %7.5f'''
return text % (x, y)
===================
[0] https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str
--
Martin A. Brown
http://linux-ip.net/
More information about the Tutor
mailing list