ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm new to python and attempting to learn python3.
I have a book that instructs
Quote:
Write a function called make_great() that modifies the list of magicians by adding the phrase the Great to each magicians name
I have written the below, but I can't make sense why after leaving the function, the list becomes []
Code:
#!/usr/bin/python3
magicians = ['useryeah', 'usercool', 'userno', 'duck']
print(magicians)
def make_great(magicians_list):
great_magicians_list = []
while magicians_list:
great_magicians_name = 'the Great ' + magicians_list.pop()
great_magicians_list.append(great_magicians_name.title())
magicians = great_magicians_list
print(magicians)
make_great(magicians)
print(magicians)
I get this output
Code:
['useryeah', 'usercool', 'userno', 'duck']
['The Great Duck', 'The Great Userno', 'The Great Usercool', 'The Great Useryeah']
[]
but I expect
Code:
['useryeah', 'usercool', 'userno', 'duck']
['The Great Duck', 'The Great Userno', 'The Great Usercool', 'The Great Useryeah']
['The Great Duck', 'The Great Userno', 'The Great Usercool', 'The Great Useryeah']
You globally declared the variable and passed it to a function, which has it's own scope. You then referenced the variable in a print statement in the global scope without returning a value for the variable from the function scope.
It has no value because no value was passed back to the global scope from the functions scope after it was modified.
Local scopes can inherit from enclosing scopes, however they cannot set a value outside their own scope unless you do so explicitly in the enclosing scope.
Confused? No worries, it makes better sense if you read the code...
bash-4.4$ python lq.py
['useryeah', 'usercool', 'userno', 'duck']
['The Great Duck', 'The Great Userno', 'The Great Usercool', 'The Great Useryeah']
['The Great Duck', 'The Great Userno', 'The Great Usercool', 'The Great Useryeah']
This, on the other hand
Code:
...
print(make_great(magicians))
print(magicians)
Produces
Quote:
['useryeah', 'usercool', 'userno', 'duck']
['The Great Duck', 'The Great Userno', 'The Great Usercool', 'The Great Useryeah']
[]
Because in the second example I didn't specifically set the global variable magicians to the return value, I just printed it.
Scoping can be a rough spot, everyone has to puzzle it out.
Last suggestion:
A better way to do this is list comprehension
Code:
#!/usr/bin/python3
magicians = ['useryeah', 'usercool', 'userno', 'duck']
print(magicians)
def make_great():
return [ 'The Great ' + x for x in magicians]
magicians = make_great()
print(magicians)
Just remember, Python is extremely cool, and you are cool for learning it
Last edited by dijetlo; 03-18-2017 at 02:12 AM.
Reason: More accurately answers the OP question
This helped tremendously!
I am reminded of bash and subprocesses when it comes to scoping.
For example,
Code:
$ a=hello; (a=goodbye); echo "$a"
hello
So I presume a analogy of return is it "escapes" the current confined scope of a function into the global scope?
I had not read yet about return (I think) but I'll test around with it
Also, I had read about list comprehension, but this made me realize how it could be relevant
I will have to test with it more too.
Eh, Sefyirs while loop ran without issue, which is pythonic enough for a beginner. His problem was scoping but he seems to have gotten a handle on that. Let's not confuse the young Jedi unnecessarily, he's just putting his light sabre together. He'll come to the pythonic force on his own, given time (assuming he doesn't choose the dark side, aka Ruby)
We should probably start a 'Stupid Python Tricks' thread if we want to get into optimizations and machine learning atomics.
On that note, Dugan, do you have any background with anaconda (the scipy environment) and if you do, can you recommend a tutorial ? I'm just getting started with it and it looks like Guidos swiss army knife (a little confusing for a mere mortal)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.