Pythonic Programming by Dmitry Zinoviev

Pythonic Programming by Dmitry Zinoviev

Author:Dmitry Zinoviev [Dmitry Zinoviev]
Language: eng
Format: epub
Publisher: Pragmatic Bookshelf
Published: 2021-09-23T00:00:00+00:00


Tip 52 Let the Caller Print

★★2.7, 3.4+ Functions or methods are usually considered as units of computation. They take the arguments (or rely on global variables, see Tip 92, ​Remember, There Are No Globals​), apply your algorithms to them, compute the results, and return them to the caller. If a function prints the calculated result instead of returning it, the caller cannot use the result for further computations. Moreover, the caller may be duped to believe that what the function returns is the result (see Tip 50, ​Make Functions Always Return Something​), but it is not. Here is a wrong way to produce a result:

​ ​def​ ​add1​(x):

​ ​print​(x+1)

​ ​# There is an implicit return None on this line!​

​ y = add1(10)

​=> ​11​

​ ​print​(y)

​=> ​None​

Here is a right way:

​ ​def​ ​add1​(x):

​ ​return​ x+1

​ y = add1(10)

​ ​print​(y)

​=> ​11​

Aside from the right and wrong ways, there is also a questionable way when a function prints the result and then returns it:

​ ​def​ ​add1​(x):

​ ​print​(x+1)

​ ​return​ x+1

​ y = add1(10)

​=> ​11​

​ ​print​(y)

​=> ​11​

This function, while functionally correct, combines computation and presentation. It always displays the result (and perhaps some other messages) and returns the result to the caller. The caller has no control over the function’s printout. Since printing is slow (see Tip 63, ​Build, Then Print​), if you call a “talkative” function in a loop, the performance of your code may significantly degrade. Also, it may be hard to see essential results buried in the sea of chatter. Let the caller of the function decide whether the returned value is worth printing.

You may still want to have an option of printing the result before returning it (say, for a very legitimate purpose of debugging). Do so by making printing optional and controllable by the caller, as explained in Tip 55, ​Pass Arguments Your Way​:

​ ​def​ ​add1​(x, debug=None):

​ ​if​ debug:

​ ​print​(x+1)

​ ​return​ x+1

​ y = add1(10)

​ y = add1(10, True)

​=> ​11​

​ ​print​(y)

​=> ​11​

As a side note, when you enable printing within a function, add an explanatory message to each printout:

​ ​print​(f​'This is x+1 in function add1: {x+1}'​)



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(55913)
What's Done in Darkness by Kayla Perrin(26528)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19009)
The Fifty Shades Trilogy & Grey by E L James(18962)
Shot Through the Heart by Mercy Celeste(18881)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16989)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16878)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16807)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16703)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16331)
The Subtle Art of Not Giving a F*ck by Mark Manson(14263)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14072)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13429)
Scorched Earth by Nick Kyme(12716)
Drei Generationen auf dem Jakobsweg by Stein Pia(10925)
Suna by Ziefle Pia(10847)
Scythe by Neal Shusterman(10271)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9479)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9317)
This is Going to Hurt by Adam Kay(9105)