Re: [Tutor] How to write the __str__ function
On 15/05/2017 01:27, Alan Gauld via Tutor wrote: On 14/05/17 19:03, Sydney Shall wrote: 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), Here endeth the first string "\n The fractional sigma (FractionalSTD)" " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) And here the second. Returning two strings separated by a comma makes it a tuple. Instead put the two values in a tuple at the end of a single concatenated string. And while at it make life easier for your self and use triple quotes: def __str__(self): return """ 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 """ % ( self.ucc, self.fractsigma_ucc) Tweak the formatting to suit. However, I'm not sure thats really a good use of __str__, I might be tempted to make that an explicit method that's called pprint() - for pretty-print - or somesuch. __str__() methods are usually a fairly cocise depiction of the objects state that you can embed in a bigger string. Maybe pprint() would look like def pprint(self): return """ 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: %s""" % self And __str__() def __str__(self): return """ The input value for the mean of UCC was %7.5f The fractional sigma (FractionalSTD) of UCC that was input was %7.5f """ % (self.ucc, self.fractsigma_ucc) Thus pprint() uses str() to create the long version while str() just gives the bare bones result. Just a thought. Here endeth the second (! -presumptious?) string. """I would like to thank you all for the five lessons. The advice of course gave me several useful ways of outputting data. I believe I now have a better understanding of output.""" Many thanks. -- Sydney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to write the __str__ function
Not exactly sure what you're trying to do here but maybe you want to join those two things instead? Because of course it's a tuple the way you have it now. Shu Sent from my iTypo (with enhanced embarrassing auto-correcting) > On May 14, 2017, at 11:03 AM, Sydney Shall wrote: > > I need some advice that I have been embarrased to ask for, because I think > that my error is so elementary. > > 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. > > 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. > > Guidance would be much appreciated. > > -- > Sydney > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to write the __str__ function
On 14/05/17 19:03, Sydney Shall wrote: > 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), Here endeth the first string > "\n The fractional sigma (FractionalSTD)" > " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) And here the second. Returning two strings separated by a comma makes it a tuple. Instead put the two values in a tuple at the end of a single concatenated string. And while at it make life easier for your self and use triple quotes: def __str__(self): return """ 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 """ % ( self.ucc, self.fractsigma_ucc) Tweak the formatting to suit. However, I'm not sure thats really a good use of __str__, I might be tempted to make that an explicit method that's called pprint() - for pretty-print - or somesuch. __str__() methods are usually a fairly cocise depiction of the objects state that you can embed in a bigger string. Maybe pprint() would look like def pprint(self): return """ 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: %s""" % self And __str__() def __str__(self): return """ The input value for the mean of UCC was %7.5f The fractional sigma (FractionalSTD) of UCC that was input was %7.5f """ % (self.ucc, self.fractsigma_ucc) Thus pprint() uses str() to create the long version while str() just gives the bare bones result. Just a thought. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to write the __str__ function
Am 14.05.2017 um 20:59 schrieb Martin A. Brown: 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) ... 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,) If you put this into your original string, it won't work. Try this very simple example: st = "abc %d" % 7 "def" # <- SyntaxError The replacements definitely belong at the end, all together. But an additional question: do you really want to get this very long text every time you need an instance of your class as a string? Even if it's put into another sentence using string formatting? Like this for example (assuming your class is called MyClass): x = MyClass(some, args) s = """This is an instance of MyClass with the value %s. It was initialized with the values some = %s, args = %s.""" % (x, some, args) print(s) Greetings, Sibylle ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to write the __str__ function
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: 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: What's going on here? thing = ("no comma here") type(thing) # you will see: thing = ("no comma here",) type(thing) # you will see: 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/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How to write the __str__ function
I need some advice that I have been embarrased to ask for, because I think that my error is so elementary. 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. 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. Guidance would be much appreciated. -- Sydney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor