Python Forum
Weird issues with return
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weird issues with return
#6
(Jan-30-2018, 01:29 AM)fad3r Wrote: How come we can't make the numbdicts be like global in scope? I used the global keyword once and someone here told me that was a huge no no. I understand why but like sometimes don't we want to take whatever transformations we have made and be able to use them everywhere? Not sure if that makes sense.

You can pass the variable you want to access as a parameter to the function along with the other parameters. The function argument name is just like another name pointing to the same object in the memory.

In [1]: powered = {}

In [2]: def power_nums(text, result):
   ...:     print(text)
   ...:     for num in range(1, 6):
   ...:         result[num] = num**2
   ...:         

In [3]: power_nums('Rise and shine', powered)
Rise and shine

In [4]: powered
Out[4]: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
Weird issues with return - by fad3r - Jan-29-2018, 10:54 PM
RE: Weird issues with return - by Mekire - Jan-29-2018, 11:11 PM
RE: Weird issues with return - by fad3r - Jan-29-2018, 11:43 PM
RE: Weird issues with return - by Mekire - Jan-29-2018, 11:48 PM
RE: Weird issues with return - by fad3r - Jan-30-2018, 01:29 AM
RE: Weird issues with return - by wavic - Jan-30-2018, 08:07 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020