[Tutor] Returning multiple objects from a function
Hello- I'm wondering how to access specific objects returned from a function when that function returns multiple objects. For example, if I have "return(list1, list2, list 3)" within a function "mainFunc()" that takes no arguments, how do I use list1, list2, and list3 outside of the function once they are returned by that function? Thank you. -Alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Returning multiple objects from a function
I understand the basics of tuples, but that formulation returned the following error: Traceback (most recent call last): File "C:\Users\Owner\Desktop\MIT\Sets\Set3.py", line 34, in list4 = tuplesresult[1] TypeError: 'NoneType' object is not subscriptable When I tried to assign "tuplesresult[1]" to the variable "list4" (after assigning tuplesresult = mainFunc(), which is the name of the function that returns the tuple in my program), the error occurred. That aside, is it all right if I just code "return list1, list2" without the parens? In that case, how would I access list1 and list2 when needed? Thanks for your help. On Mon, Jul 2, 2012 at 4:11 PM, Walter Prins wrote: > On 2 July 2012 23:55, Alexander Q. wrote: > > Hello- I'm wondering how to access specific objects returned from a > function > > when that function returns multiple objects. > > > > For example, if I have "return(list1, list2, list 3)" within a function > > When you have: > return (list1, list2, list3) > > ... you're actually returning a single tuple object. Read about > tuples in the documentation. To access an item from a tuple you index > into it, e.g. > > tupleresult = myfunc() > x = tupleresult[0] > > ... for example > > Walter. > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Returning multiple objects from a function
On Mon, Jul 2, 2012 at 8:56 PM, Dave Angel wrote: > On 07/02/2012 10:23 PM, Alexander Q. wrote: > > I understand the basics of tuples, but that formulation returned the > > following error: > > > > Traceback (most recent call last): > > File "C:\Users\Owner\Desktop\MIT\Sets\Set3.py", line 34, in > > list4 = tuplesresult[1] > > TypeError: 'NoneType' object is not subscriptable > > > > When I tried to assign "tuplesresult[1]" to the variable "list4" (after > > assigning tuplesresult = mainFunc(), which is the name of the function > that > > returns the tuple in my program), the error occurred. That aside, is it > all > > right if I just code "return list1, list2" without the parens? In that > > case, how would I access list1 and list2 when needed? > > That's no different: list1, list2 is a tuple of size 2. > > Thanks for your help. > > > You top-posted, which loses all the context of the earlier messages. > Please put your new message AFTER the part you're quoting > > Anyway, you never showed the function mainFunc(), but from the error > traceback message, it didn't return anything, which means None. > > Chances are you have something like: > > def mainFunc(): > if something: > return 4, 12 > > > And if the if fails, the function will return None. To fix that, make > sure all paths through the function return a similar object, generally a > tuple of the same size. > > > > -- > > DaveA > > Ok thanks- I will try that. -Alex P.S. Let me know if this message is sent incorrectly (I scrolled down to the bottom of the text box to type this, as opposed to writing the message at the top of the text box. I am guessing this is what is meant by "top-posting."). ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Regular expressions: findall vs search
I'm a bit confused about extracting data using re.search or re.findall. Say I have the following code: tuples = re.findall(r'blahblah(\d+)yattayattayatta(\w+)moreblahblahblah(\w+)over', text) So I'm looking for that string in 'text', and I intend to extract the parts which have parentheses around them. And it works: the variable "tuples", which I assigned to get the return of re.findall, returns a tuple list, each 'element' therein being a tuple of 3 elements (which is what I wanted since I had 3 sets of parentheses). My question is how does Python know to return just the part in the parentheses and not to return the "blahblah" and the "yattayattayatta", etc...? The 're.search' function returns the whole thing, and if I want just the parentheses parts, I do tuples.group(1) or tuples.group(2) or tuples.group(3), depending on which set of parentheses I want. Does the re.findall command by default ignore anything outside of the parentheses and only return the parentheses as a grouping withing one tuple (i.e., the first element in "tuples" would be, as it is, a list comprised of 3 elements corresponding respectively to the 1st, 2nd, and 3rd parentheses)? Thank you for reading. -Alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Calling a function does not return what I want it to return
I have this little program that is supposed to calculate how many diagonals a polygon of x sides has, but it does not return what I have in the "return" part of the function when I call it. Here is the code: def num_diag(var): ans = 0 if var <= 3: print("No diagonals.") else: for i in range(num_sides - 3): ans = ans + i return (((var - 3)*2) + ans) num_sides = (int(raw_input("Enter sides: "))) num_diag(num_sides) Any suggestions as to what is going on? When I run it, it prompts me for the number of sides, and that's it. Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Calling a function does not return what I want it to return
On Thu, Jul 19, 2012 at 4:21 PM, Dave Angel wrote: > On 07/19/2012 06:58 PM, Alexander Q. wrote: > > I have this little program that is supposed to calculate how many > diagonals > > a polygon of x sides has, but it does not return what I have in the > > "return" part of the function when I call it. Here is the code: > > > > def num_diag(var): > > ans = 0 > > if var <= 3: > > print("No diagonals.") > > else: > > for i in range(num_sides - 3): > > ans = ans + i > > > > return (((var - 3)*2) + ans) > > > > num_sides = (int(raw_input("Enter sides: "))) > > num_diag(num_sides) > > > > > > Any suggestions as to what is going on? When I run it, it prompts me for > > the number of sides, and that's it. > > Thanks. > > > > > > You never use the return value. Try assigning it, and printing it. > > result = num_diag(num_sides) > print("final answer=", result) > > -- > > DaveA > > That did it- thanks Dave! -Alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Newline question
I'm following the tutorial from python.org ( http://docs.python.org/tutorial/introduction.html) and am having a few indiscrepancies regarding the new line command. The tutorial says that this code hello = "This is a rather long string containing\n\ several lines of text just as you would do in C.\n\ **Note that whitespace at the beginning of the line is\ significant." should yield this output: This is a rather long string containing several lines of text just as you would do in C. Note that whitespace at the beginning of the line is significant. When I run it in the interpreter however, I get the following output: 'This is a rather long string containing\nseveral lines of text just as you would do in C.\nNote that whitespace at the beginning of the line is significant.' The interpreter is not reading the "\n" as new lines but is instead printing them. If I just type something like hello = "This is a rather long string containing \ several lines of text." the output is all on one line, like this: "This is a rather long string containing several lines of text." So I know how to combine code that spans multiple lines to be outputted on one line, but I do not know how to make a newline appear in the output upon command (on account of the interpreter not reading the \n). Any suggestions as to why my output varies from the output in the tutorial? I am running a 2.7 version of Python, btw, and the tutorial is running a higher version I believe (3. something). Thank you. -Alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newline question
On Fri, Aug 3, 2012 at 1:40 PM, Jerry Hill wrote: > On Fri, Aug 3, 2012 at 4:18 PM, Alexander Q. wrote: > > I'm following the tutorial from python.org > > (http://docs.python.org/tutorial/introduction.html) and am having a few > > indiscrepancies regarding the new line command. > > > > The tutorial says that this code > > > > hello = "This is a rather long string containing\n\ > > several lines of text just as you would do in C.\n\ > > Note that whitespace at the beginning of the line is\ > > significant." > > > > should yield this output: > > > > This is a rather long string containing > > several lines of text just as you would do in C. > > Note that whitespace at the beginning of the line is significant. > > You left out the other line of code in the tutorial, which says you > need to do print(hello) to the the output that is described. Did you > do that? If so, it should work fine. If not, what did you do > instead? If you just typed: > > >>>hello > > at the interpreter prompt, then you are actually seeing the equivalent > of print(repr(hello)), instead of print(hello). > > Can you copy and paste your session for us? > > Jerry > That was it Jerry- when I typed in "print hello" instead of just "hello", the output was exactly like the one in the tutorial. Alternatively, I could accomplish the same type of output by using triple quotes around the same text, except that I would have to format it manually if I want it to come out looking the same way as it did when using "\n" in the previous example? Thanks again for your help. The way I understand it from your explanation is that "hello" does a literal output of everything typed without processing the escape backslashes, while "print hello" does process them. -Alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newline question
On Fri, Aug 3, 2012 at 2:09 PM, Alexander Q. wrote: > > > On Fri, Aug 3, 2012 at 1:40 PM, Jerry Hill wrote: > >> On Fri, Aug 3, 2012 at 4:18 PM, Alexander Q. >> wrote: >> > I'm following the tutorial from python.org >> > (http://docs.python.org/tutorial/introduction.html) and am having a few >> > indiscrepancies regarding the new line command. >> > >> > The tutorial says that this code >> > >> > hello = "This is a rather long string containing\n\ >> > several lines of text just as you would do in C.\n\ >> > Note that whitespace at the beginning of the line is\ >> > significant." >> > >> > should yield this output: >> > >> > This is a rather long string containing >> > several lines of text just as you would do in C. >> > Note that whitespace at the beginning of the line is significant. >> >> You left out the other line of code in the tutorial, which says you >> need to do print(hello) to the the output that is described. Did you >> do that? If so, it should work fine. If not, what did you do >> instead? If you just typed: >> >> >>>hello >> >> at the interpreter prompt, then you are actually seeing the equivalent >> of print(repr(hello)), instead of print(hello). >> >> Can you copy and paste your session for us? >> >> Jerry >> > > That was it Jerry- when I typed in "print hello" instead of just "hello", > the output was exactly like the one in the tutorial. Alternatively, I could > accomplish the same type of output by using triple quotes around the same > text, except that I would have to format it manually if I want it to come > out looking the same way as it did when using "\n" in the previous example? > Thanks again for your help. The way I understand it from your explanation > is that "hello" does a literal output of everything typed without > processing the escape backslashes, while "print hello" does process them. > > -Alex > > Yes, thanks Matt- I realized my mistake soon after sending the email. -Alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor