0

Ho! I am in need of thine assistance fellow followers of the programming discipline. I have here a devil-ish piece of parchment, of which a destitute acquaintance of mine proposed for me to solve, but alas, I was not able to correctly do so, much to my heart's dismay:

#!/usr/bin/python3

def bestow(tidings, scroll=[]):
  scroll.append("I bestow to you: " + tidings)
  return scroll

I = bestow("Alexis Texas")
II = bestow("Mia Khalifa", [])
III = bestow("Asa Akira")

print ("{0}, {1}, {2}".format(I, II, III))

I desire an understanding of why thy code spits out the following result:

['I bestow to you: Alexis Texas', 'I bestow to you: Asa Akira'], ['I bestow to you: Mia Khalifa'], ['I bestow to you: Alexis Texas', 'I bestow to you: Asa Akira']

I happened to have previously thought that def bestow would be granted its default scroll parameter each time it is called, and as such, a new scroll would be instantiated for every call to thine function, as such this is what I thought would be the result of the print commandment:

['I bestow to you: Alexis Texas'], ['I bestow to you: Mia Khalifa'], ['I bestow to you: Asa Akira']

but that is evidently far from the truth.

Can someone please help me see with eyes unclouded by ignorance? I thanketh thee ahead of its due.

  • http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables – jkdev Jul 05 '16 at 01:05
  • http://effbot.org/zone/default-values.htm – jkdev Jul 05 '16 at 01:08
  • "Default parameter values are evaluated when the function definition is executed," that is, when the function is first defined. Not when the function is called or invoked. http://docs.python.org/reference/compound_stmts.html#function-definitions – jkdev Jul 05 '16 at 01:11
  • "[Marked as duplicate](https://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument?lq=1)," 'tis the final judgment that hast pronounced upon this answer. Never fear! Olde questions of the Stack Overflow serve as the mighty foundation upon which knowledge shalt be built and extended. 'Twas always so, and 'tis how society finds itself thrust forward through progress in the sciences and computational technologies. Quoth Sir Isaac Newton, "[If I have seen further it is by standing on the shoulders of Giants.](http://www.isaacnewton.org.uk/essays/Giants)" – jkdev Jul 05 '16 at 01:22

0 Answers0