![]() I am very surprised now how i couldn’t understand it from your function’s name.- Sorry for these, really.Ībout your main question, it seems, I gave you some classic methods-but code was wrong because I understood wrong what you exactly wanted to do with it.- and your question wasn’t about it at all. Actually, if block a bit confused me and recursive function was sending the list without its last value to itself as an argument. Sorry for confusing sentence and misunderstanding.Īnd, when we come to your code It seems I couldn’t understand well what you exactly wanted to do. I thought that you thought it is a totally different list. Not a copied list”, I have meant that slice is inside the list, and any changes made on that slice will effect the main list. When I was saying “slicing returns a list which is inside the original list. Return buddle_sort(lst)#changed to prevent returning None when if block isn't executed [EDIT:I have just realised that doing just this could also make the code properly worked: from typing import List Return lst # this is the same slice with lstĭef buddle_sort(lst: List): #process-manager main buddle_sort method. You can make it more useful and easier with two different sort functions- one is the process manager (main) function, and one is the process itself. You can use the global keyword with a global variable but that probably won’t be pretty. To work with them, you need to use an external variable, not a parameter. So, it’s hard to work with recursive functions. You can see this post about recursive functions.(please see it before reading the rest of my post). ![]() And, that kind of functions behave a bit more different than what you expect. Right now, we can apply the same changes to your code, like this: from typing import Listīut this code will also not work properly, it will just print None and give an error like, ‘you can not use len() on NoneType object’. MyNumber = abc(myNumber) #assigning the value, which our function returns, to our original variable. To succeed that you should pass (assign) that value to your original variable: def abc(number): This code still prints 10, because return keyword just assigns the value of our function not automatically updates our original variable. Let’s try working with it on our example code: def abc(number): Because, its normal syntax is: return something ![]() In your code, I can see that you have also used a return but it actually returns None. To change your original variable, you can use the return keyword. Number -= 5 #this change is made on the parameter, not on the original variable. To understand this better, you can try running this basic code: def abc(number): If you are working with parameters, you must know that changes made at a parameter doesn’t effect the original variable you pass into it. But, in your code there is a function which edits the parameter-list. Hello, No, slicing returns a list which is inside the original list.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |