Re: [Tutor] Why begin a function name with an underscore
Steven D'Aprano wrote: On 28/08/12 20:00, Peter Otten wrote: [...] The differences to _validate_int() are subtle: class S(str): ... def __eq__(self, other): return True ... def __ne__(self, other): return False ... def __add__(self, other): return self ... vi(S(42)) Traceback (most recent call last): File stdin, line 1, inmodule File stdin, line 3, in vi TypeError _validate_int(S(42)) It seems to me that, in some ways at least, S(42) is a string that quacks like an int (if you add it to zero, you get itself), and therefore under duck-typing rules you might be justified in calling it an int. Of course, if you actually try to use it as an int, it fails to walk like an int or swim like an int, so this is a case of garbage in, garbage out. The class was written to demonstrate that your and my implementation of an integer check may give different results, if only in corner cases with little practical relevance. There is always tension between safety and freedom. D'accord. I tend to err on the side of freedom. sum([a, b, c], ) Traceback (most recent call last): File stdin, line 1, in module TypeError: sum() can't sum strings [use ''.join(seq) instead] gives me the creeps even though it'd never occur to me to actually use sum() to join a sequence of strings. A strict type-check will increase safety by preventing many GIGO errors, but it also reduces freedom to use your own WholeNumber type. But freedom to use your own numeric types is also freedom to abuse types like S above. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
On Thu, Aug 30, 2012 at 4:44 AM, Peter Otten __pete...@web.de wrote: sum([a, b, c], ) Traceback (most recent call last): File stdin, line 1, in module TypeError: sum() can't sum strings [use ''.join(seq) instead] gives me the creeps even though it'd never occur to me to actually use sum() to join a sequence of strings. class Start(object): def __add__(self, other): return other sum(['Bypassing', ' nanny', ' typecheck...'], Start()) 'Bypassing nanny typecheck...' FTFY (It goes without saying: never do this.) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] understanding pydoc try
What does the first line from `pydoc try` actually mean? This does not look like the syntax that one is supposed to use. try_stmt ::= try1_stmt | try2_stmt I can write simple statements as shown below, but I want to actually understand what I am doing. try: import io print(importing io) except ImportError: print(nothing to import) foo = None try: import somefunctionthatdoesnotexist print(importing ...) except ImportError: print(nothing to import) foo = None ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding pydoc try
On 30 August 2012 15:30, John Maclean jaye...@gmail.com wrote: What does the first line from `pydoc try` actually mean? This does not look like the syntax that one is supposed to use. try_stmt ::= try1_stmt | try2_stmt looks like part of the python language reference. It goes a little further and explains what try1_stmt and try2_stmt actually suppose to mean: http://docs.python.org/reference/compound_stmts.html#the-try-statement try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= try : suite (except [expression [(as | ,) target]] : suite)+ [else : suite] [finally : suite] try2_stmt ::= try : suite finally : suite Let me try to rephrase it: a try statement is either of try-except-else-finally or of try-finally form. This notation is used to formally describe language syntax: http://docs.python.org/reference/introduction.html#notation best, Michael ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding pydoc try
On 08/30/2012 09:30 AM, John Maclean wrote: What does the first line from `pydoc try` actually mean? This does not look like the syntax that one is supposed to use. try_stmt ::= try1_stmt | try2_stmt You're looking at the first of three BNF statements. BNF (Backus Naur Form, or something like that) is a way of describing a grammar. i'll quote the whole thing here, and try to explain it. The following is from Python 3.2's pydoc: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= try : suite (except [expression [as target]] : suite)+ [else : suite] [finally : suite] try2_stmt ::= try : suite finally : suite The first statement says that a try_stmt is one or the other of two formats. This simply says there are two syntaxes you can use, depending on what try features you want. The second lists the (most common, i expect) syntax. It has a literal try token, followed by a literal colon token, followed by a suite of statements (that's defined elsewhere, but would include simple statements, if statements, and so on. It wouldn't include def or class, presumably). Then there are one or more except clauses. Note the trailing + which means this element may be repeated, but must be present at least once. Then there is an optional else clause. Then an optional finally clause. These must be present in the specific order stated above. And you can't (for example) have an else without an except, because except is one or more times. The second syntax does not include the except nor else clauses. Is that clearer? -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding pydoc try
On 30/08/12 23:30, John Maclean wrote: What does the first line from `pydoc try` actually mean? This does not look like the syntax that one is supposed to use. try_stmt ::= try1_stmt | try2_stmt That's a description of the Python grammar in some variation of Backus-Naur Form. In English, it means: A try statement is either a try1 statement or a try2 statement. Presumably then there will be a definition of try1_stmt and try2_stmt, also in BNF form, probably in terms of other statements, until eventually the syntax of try statements is fully defined. http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form#Example http://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form I can write simple statements as shown below, but I want to actually understand what I am doing. try: import io print(importing io) except ImportError: print(nothing to import) foo = None This first block attempts to import the io module, and then print importing io. If the import process fails, an exception is raised. But since the io module does exist and can be imported, nothing happens and execution happily goes on to the part *after* the except clause: try: import somefunctionthatdoesnotexist print(importing ...) except ImportError: print(nothing to import) foo = None This time, since somefunction blah blah probably doesn't exist, the import process does fail, and an exception is raised, interrupting normal execution of the code. Instead of the next line running, execution of your code halts and Python signals an ImportError. However, then the except ImportError line takes over and catches the exception. nothing to import is printed and foo is set to None. Does that help? -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding pydoc try
On 08/30/2012 03:05 PM, Dave Angel wrote: On 08/30/2012 09:30 AM, John Maclean wrote: What does the first line from `pydoc try` actually mean? This does not look like the syntax that one is supposed to use. try_stmt ::= try1_stmt | try2_stmt You're looking at the first of three BNF statements. BNF (Backus Naur Form, or something like that) is a way of describing a grammar. i'll quote the whole thing here, and try to explain it. The following is from Python 3.2's pydoc: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= try : suite (except [expression [as target]] : suite)+ [else : suite] [finally : suite] try2_stmt ::= try : suite finally : suite The first statement says that a try_stmt is one or the other of two formats. This simply says there are two syntaxes you can use, depending on what try features you want. The second lists the (most common, i expect) syntax. It has a literal try token, followed by a literal colon token, followed by a suite of statements (that's defined elsewhere, but would include simple statements, if statements, and so on. It wouldn't include def or class, presumably). Then there are one or more except clauses. Note the trailing + which means this element may be repeated, but must be present at least once. Then there is an optional else clause. Then an optional finally clause. These must be present in the specific order stated above. And you can't (for example) have an else without an except, because except is one or more times. The second syntax does not include the except nor else clauses. Is that clearer? Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another set TLA that I don't need to know ;-) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding pydoc try
On 08/30/2012 10:43 AM, John Maclean wrote: On 08/30/2012 03:05 PM, Dave Angel wrote: snip Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another set TLA that I don't need to know ;-) I learned BNF in about 1972. I've used about 35 languages since (not counting hobby ones). It can clarify a new language better than many paragraphs of description. But I've found that it's seldom completely rigorous. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding pydoc try
On 30-Aug-12 08:22, Dave Angel wrote: On 08/30/2012 10:43 AM, John Maclean wrote: On 08/30/2012 03:05 PM, Dave Angel wrote: snip Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another set TLA that I don't need to know ;-) I learned BNF in about 1972. I've used about 35 languages since (not counting hobby ones). It can clarify a new language better than many paragraphs of description. But I've found that it's seldom completely rigorous. True, usually because people aren't as careful writing it as they are real code that needs to be executed by something. Maybe it would help to start by describing your grammar to YACC, getting it to work, and then expressing that back out as BNF (or just leaving it in YACC code). -- Steve Willoughby / st...@alchemy.com A ship in harbor is safe, but that is not what ships are built for. PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding pydoc try
On 08/30/2012 11:26 AM, Steve Willoughby wrote: On 30-Aug-12 08:22, Dave Angel wrote: On 08/30/2012 10:43 AM, John Maclean wrote: On 08/30/2012 03:05 PM, Dave Angel wrote: snip Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another set TLA that I don't need to know ;-) I learned BNF in about 1972. I've used about 35 languages since (not counting hobby ones). It can clarify a new language better than many paragraphs of description. But I've found that it's seldom completely rigorous. True, usually because people aren't as careful writing it as they are real code that needs to be executed by something. Maybe it would help to start by describing your grammar to YACC, getting it to work, and then expressing that back out as BNF (or just leaving it in YACC code). There's another reason, that I usually assumed to be the case. It usually happens at a place where the grammar is particularly tricky, and where the only valid thing to do in BNF is to list lots of cases (as the one in this thread lists two). So I assumed the BNF was more-or-less deliberately dumbed down to make it more legible. I like your explanation better, though. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding pydoc try
On 30/08/12 15:43, John Maclean wrote: Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another set TLA that I don't need to know ;-) Actually, BNF is one of those useful skills for any programmer because almost every language is 'formally' described using it - at least since the days of Algol, for which it was invented. A simplified version of it is also used to define most command line tools and their arguments so its definitely worth learning, at least the basics. It can save a lot of typing when you want to precisely specify the allowed grammar in a problem. There are tools which can translate BNF like text into something close to code, which is useful if you ever have to define your own programming language. Admittedly not something most programmers ever need to do, but it does happen occasionally that its the easiest way to solve a problem. (The so-called mini-language design pattern) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding pydoc try
On 08/30/2012 05:15 PM, Alan Gauld wrote: On 30/08/12 15:43, John Maclean wrote: Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another set TLA that I don't need to know ;-) Actually, BNF is one of those useful skills for any programmer because almost every language is 'formally' described using it - at least since the days of Algol, for which it was invented. A simplified version of it is also used to define most command line tools and their arguments so its definitely worth learning, at least the basics. It can save a lot of typing when you want to precisely specify the allowed grammar in a problem. There are tools which can translate BNF like text into something close to code, which is useful if you ever have to define your own programming language. Admittedly not something most programmers ever need to do, but it does happen occasionally that its the easiest way to solve a problem. (The so-called mini-language design pattern) My main issue is that I am a sysadmin and not a programmer. I am aware of pydoc but not of BNF. So I was a bit taken aback when I saw the BNF syntax. It was obvious to me that syntax of the try statements were not python syntax but had no clue how to parse it. BTW - where in pydoc is it mentioned, (or anywhere else for that matter), to refer to BNF? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding pydoc try
On 30/08/12 17:21, John Maclean wrote: My main issue is that I am a sysadmin and not a programmer. I am aware of pydoc but not of BNF. So I was a bit taken aback when I saw the BNF syntax. It was obvious to me that syntax of the try statements were not python syntax but had no clue how to parse it. BTW - where in pydoc is it mentioned, (or anywhere else for that matter), to refer to BNF? If you are writing (or reading!) code you are a programmer! :-) Michael already gave a link to the notation page on the web site which does explicitly mention BNF but, to be honest it would not be surprising if it didn't. It would be like specifically saying that a web page was written in HTML, nowadays its often just assumed that anyone creating web pages knows about HTML... Similarly languages are usually specified in some approximation of BNF. The link was: http://docs.python.org/reference/introduction.html#notation -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
Steven D'Aprano wrote: On 28/08/12 19:02, Peter Otten wrote: Personally, I'm a big fan of ducktyping, so I would probably remove the check completely and live with the consequences: pyprimes._validate_int = lambda x: None pyprimes.isprime_naive(8.5) True garbage-in, garbage-out -- so what. Duck-typing means that if an object implements the interface for an int, or if it behaves like an int (not quite the same thing!), you can safely treat it as an int. 8.5 is not an int, and isn't a prime number, so why is isprime claiming it is prime? That's a bug -- or rather, it *would* be a bug except you deliberately broke it. No, I demonstrated the limitations of an alternate implementation, one that I prefer in spite of these limitations. There's a strong argument to be made that isprime should accept any number and return False for those which aren't integers, rather than raise an exception. That's reasonable behaviour. But silently returning garbage? That's the worst thing you could do, *far* worse than an unexpected exception or an overly-strict type-check. Allowing floats for a primality test is a can of worms anyway. You will inevitably run out of significant digits: from pyprimes import isprime_naive as isprime 2**61-1 2305843009213693951 isprime(2305843009213693951.0) False isprime(2305843009213693951) True int(2305843009213693951.0) 2305843009213693952 Errors should occur as close as possible to where they are introduced, and functions shouldn't return garbage -- they should either be correct, or raise an exception. Returning some junk value is bad. Imagine if you accidentally called len(None), and instead of raising, it returned 17. That's how bad it is. I'm reminded of this quote: I find it amusing when novice programmers believe their main job is preventing programs from crashing. ... More experienced programmers realize that correct code is great, code that crashes could use improvement, but incorrect code that doesn’t crash is a horrible nightmare. -- CD Smith I think it depends on what you expect from a function. If you want it to execute a particular algorithm it is OK not to impose limitations on the input. As a building block for an actual application I'd go with a whitelist and require a numbers.Integral or even int (assuming Python 3) instance. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
On 31/08/12 04:07, Peter Otten wrote: Allowing floats for a primality test is a can of worms anyway. You will inevitably run out of significant digits: [snip] Yes, I had more or less come to the same conclusion earlier. The problem is that although sufficiently large floats are all integer-valued, they're not necessarily the integer value that you think: py 1e100 == 10**100 False [...] I find it amusing when novice programmers believe their main job is preventing programs from crashing. ... More experienced programmers realize that correct code is great, code that crashes could use improvement, but incorrect code that doesn’t crash is a horrible nightmare. -- CD Smith I think it depends on what you expect from a function. If you want it to execute a particular algorithm it is OK not to impose limitations on the input. This makes no sense to me. Algorithms depend on valid input, both in real life and in computing. If you accept invalid input, you're not executing the algorithm that you want, you're executing a *different* algorithm. The algorithm for make a soft boiled egg assumes you have an egg, not a brick, and further that it is a chicken egg, not an ostrich egg or a flea's egg. The algorithm for sort a list requires a list, not a dict. If by some fluke your code runs all the way to the end when you call it with a dict argument, you haven't *sorted a list*. You've just wasted CPU cycles for no good reason. The algorithm for is this a prime number? depends on the argument being a positive, integer-valued number. Accepting any garbage input and just *hoping* that the function will eventually raise an *appropriate* exception doesn't seem very wise to me. You can boil that ostrich egg for three minutes too, but it won't taste very nice when you go to eat it. As a building block for an actual application I'd go with a whitelist and require a numbers.Integral or even int (assuming Python 3) instance. I am supporting Python 2.5 onwards, so I can't use numbers.Integral. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
On Thu, Aug 30, 2012 at 2:07 PM, Peter Otten __pete...@web.de wrote: Allowing floats for a primality test is a can of worms anyway. You will inevitably run out of significant digits: Allowing floats can also lead to type errors for operations that require an integral type, but at least they're easier to catch with proper testing. For example, line 323 will raise a TypeError if n is a float: http://code.google.com/p/pyprimes/source/browse/src/pyprimes.py#323 1.0 == 1, but range(1.0) is not allowed and neither is [0,1][1.0]. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] using multiprocessing efficiently to process large data file
Hi Guys I have a with few million lines. I want to process each block of 8 lines and from my estimate my job is not IO bound. In other words it takes a lot more time to do the computation than it would take for simply reading the file. I am wondering how can I go about reading data from this at a faster pace and then farm out the jobs to worker function using multiprocessing module. I can think of two ways. 1. split the split and read it in parallel(dint work well for me ) primarily because I dont know how to read a file in parallel efficiently. 2. keep reading the file sequentially into a buffer of some size and farm out a chunks of the data through multiprocessing. Any example would be of great help. Thanks! -Abhi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using multiprocessing efficiently to process large data file
I have a with few million lines. I want to process each block of 8 lines and from my estimate my job is not IO bound. In other words it takes a lot more time to do the computation than it would take for simply reading the file. I am wondering how can I go about reading data from this at a faster pace and then farm out the jobs to worker function using multiprocessing module. I can think of two ways. 1. split the split and read it in parallel(dint work well for me ) primarily because I dont know how to read a file in parallel efficiently. 2. keep reading the file sequentially into a buffer of some size and farm out a chunks of the data through multiprocessing. Any example would be of great help. The general logic should work, but did not test with a real file. with open( file, 'r' ) as f: data = f.readlines() iterdata = iter(data ) grouped_data =[] for d in iterdata: l = [d, next(iterdata)] # make this list 8 elements instead grouped_data.append( l ) # batch_process on grouped data Theoretically you might be able to call next() directly on the file without doing readlines(). Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using multiprocessing efficiently to process large data file
On 30/08/12 23:19, Abhishek Pratap wrote: I am wondering how can I go about reading data from this at a faster pace and then farm out the jobs to worker function using multiprocessing module. I can think of two ways. 1. split the split and read it in parallel(dint work well for me ) primarily because I dont know how to read a file in parallel efficiently. Can you show us what you tried? It's always easier to give an answer to a concrete example than to a hypethetical scenario. 2. keep reading the file sequentially into a buffer of some size and farm out a chunks of the data through multiprocessing. This is the model I've used. In pseudo code for line, data in enumerate(file): while line % chunksize: chunk.append(data) launch_subprocess(chunk) I'd tend to go for big chunks - if you have a million lines in your file I'd pick a chunksize of around 10,000-100,000 lines. If you go too small the overhead of starting the subprocess will swamp any gains you get. Also remember the constraints of how many actual CPUs/Cores you have. Too many tasks spread over too few CPUs will just cause more swapping. Any less than 4 cores is probably not worth the effort. Just maximise the efficiency of your algorithm - which is probably worth doing first anyway. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Printing a list as a column
Does anyone know how to print a list in a form of a column instead of a row? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 102, Issue 98
On Aug 30, 2012, at 8:15 PM, tutor-requ...@python.org wrote: Message: 6 Date: Fri, 31 Aug 2012 00:15:41 + From: Ashley Fowler afowl...@broncos.uncfsu.edu To: tutor@python.org tutor@python.org Subject: [Tutor] Printing a list as a column Message-ID: 6962c976ae76ac4298cbf6fd6d0c63561f37c...@bl2prd0710mb363.namprd07.prod.outlook.com Content-Type: text/plain; charset=iso-8859-1 Does anyone know how to print a list in a form of a column instead of a row? Please give an example of what you mean here. For example, you might want to see: ['a', 'b', 'c', 'd', 'e'] My guess is that what you want is for the list ['a', 'b', 'c', 'd', 'e'] to print out as: a b c d e Yes? Are you familiar with the join method of strings? Try it out. If you have trouble, let us know :) Take care, Don ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Printing a list as a column
On 31/08/2012 01:15, Ashley Fowler wrote: Does anyone know how to print a list in a form of a column instead of a row? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor This simple? mylist=range(5) for x in mylist: ... print x ... 0 1 2 3 4 -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Printing list in a Column
Can anyone help me edit this code below to return the list in the form of a column instead of a row? def printList(): list1 = input(Insert a list) list = [list1] print (list) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Scheme
This is a problem using the Scheme programming...Can anybody help me with this problem? 2. Write a procedure (sphere r) that takes the radius of a sphere as the value of its input parameter and returns the volume of that sphere given by the formula: (4/3)π(r^3). Use (require scheme/math) or (require racket/math) to load the math library containing the pi constant. Be sure to use cube from problem (1) to find the cube of r (r^3). Tests: (sphere 2) == 33.51 ... (sphere 5.3) == 623.61 ... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Scheme
On 31/08/12 09:32, Ashley Fowler wrote: This is a problem using the Scheme programming...Can anybody help me with this problem? Probably, but not likely to be anyone here. Have you considered asking on a Lisp or Scheme mailing list or discussion forum? -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Printing list in a Column
On 31/08/2012 02:12, Ashley Fowler wrote: Can anyone help me edit this code below to return the list in the form of a column instead of a row? def printList(): list1 = input(Insert a list) list = [list1] print (list) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Use one of the two answers that you got roughly half an hour before you posted this? -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
On 31/08/12 06:57, eryksun wrote: On Thu, Aug 30, 2012 at 2:07 PM, Peter Otten__pete...@web.de wrote: Allowing floats for a primality test is a can of worms anyway. You will inevitably run out of significant digits: Allowing floats can also lead to type errors for operations that require an integral type, but at least they're easier to catch with proper testing. For example, line 323 will raise a TypeError if n is a float: Good catch, thank you. And that's why the module is still in alpha. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Scheme
And this looks like a homework problem, too. It is against etiquette to just ask for the solution to homework on ANY forum, message board, or mailing list. Since it's been given to you as homework, you're supposed to give it enough thoughts, and (hopefully) come up with your solution. Even when you go to the Lisp or Scheme mailing list, you should at least show what you have tried, paste your own code, and tell them where you are stuck. On 31 August 2012 07:32, Ashley Fowler afowl...@broncos.uncfsu.edu wrote: This is a problem using the Scheme programming...Can anybody help me with this problem? 2. Write a procedure (sphere r) that takes the radius of a sphere as the value of its input parameter and returns the volume of that sphere given by the formula: (4/3)π(r^3). Use (require scheme/math) or (require racket/math) to load the math library containing the pi constant. Be sure to use cube from problem (1) to find the cube of r (r^3). Tests: (sphere 2) == 33.51 ... (sphere 5.3) == 623.61 ... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Scheme
Understood. Disregard this problem. From: tutor-bounces+afowler2=broncos.uncfsu@python.org [tutor-bounces+afowler2=broncos.uncfsu@python.org] on behalf of Kal Sze [swordan...@gmail.com] Sent: Friday, August 31, 2012 1:32 AM To: tutor@python.org Subject: Re: [Tutor] Scheme And this looks like a homework problem, too. It is against etiquette to just ask for the solution to homework on ANY forum, message board, or mailing list. Since it's been given to you as homework, you're supposed to give it enough thoughts, and (hopefully) come up with your solution. Even when you go to the Lisp or Scheme mailing list, you should at least show what you have tried, paste your own code, and tell them where you are stuck. On 31 August 2012 07:32, Ashley Fowler afowl...@broncos.uncfsu.edumailto:afowl...@broncos.uncfsu.edu wrote: This is a problem using the Scheme programming...Can anybody help me with this problem? 2. Write a procedure (sphere r) that takes the radius of a sphere as the value of its input parameter and returns the volume of that sphere given by the formula: (4/3)π(r^3). Use (require scheme/math) or (require racket/math) to load the math library containing the pi constant. Be sure to use cube from problem (1) to find the cube of r (r^3). Tests: (sphere 2) == 33.51 ... (sphere 5.3) == 623.61 ... ___ Tutor maillist - Tutor@python.orgmailto:Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Scheme
On 31/08/2012 00:32, Ashley Fowler wrote: This is a problem using the Scheme programming...Can anybody help me with this problem? 2. Write a procedure (sphere r) that takes the radius of a sphere as the value of its input parameter and returns the volume of that sphere given by the formula: (4/3)π(r^3). Use (require scheme/math) or (require racket/math) to load the math library containing the pi constant. Be sure to use cube from problem (1) to find the cube of r (r^3). Tests: (sphere 2) == 33.51 ... (sphere 5.3) == 623.61 ... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Have I got this right? You're asking a question about the Scheme programming language on a Python mailing list, yes? -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Scheme
yes From: tutor-bounces+afowler2=broncos.uncfsu@python.org [tutor-bounces+afowler2=broncos.uncfsu@python.org] on behalf of Mark Lawrence [breamore...@yahoo.co.uk] Sent: Friday, August 31, 2012 1:32 AM To: tutor@python.org Subject: Re: [Tutor] Scheme On 31/08/2012 00:32, Ashley Fowler wrote: This is a problem using the Scheme programming...Can anybody help me with this problem? 2. Write a procedure (sphere r) that takes the radius of a sphere as the value of its input parameter and returns the volume of that sphere given by the formula: (4/3)π(r^3). Use (require scheme/math) or (require racket/math) to load the math library containing the pi constant. Be sure to use cube from problem (1) to find the cube of r (r^3). Tests: (sphere 2) == 33.51 ... (sphere 5.3) == 623.61 ... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Have I got this right? You're asking a question about the Scheme programming language on a Python mailing list, yes? -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Lambda?? Whaaaaat?
I'm fairly new to python having recently completed LPTHW. While randomly reading stack overflow I've run into lambda but haven't seen an explanation of what that is, how it works, etc. Would anyone care to point me in the right direction? Thanks in advance Scott ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lambda?? Whaaaaat?
Hey Scott, Always refer to google and the python docs first. from http://docs.python.org/tutorial/controlflow.html#lambda-forms 4.7.5. Lambda Forms By popular demand, a few features commonly found in functional programming languages like Lisp have been added to Python. With the lambdahttp://docs.python.org/reference/expressions.html#lambdakeyword, small anonymous functions can be created. Here’s a function that returns the sum of its two arguments: lambda a, b: a+b. Lambda forms can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda forms can reference variables from the containing scope: def make_incrementor(n):... return lambda x: x + n... f = make_incrementor(42) f(0)42 f(1)43 and also, this looks interesting as well: http://www.diveintopython.net/power_of_introspection/lambda_functions.html I haven't used lambdas but a few times, so a little google research can go a long way. Sincerely, David Hutto http://hitwebdevelopment.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lambda?? Whaaaaat?
On 08/30/2012 11:39 PM, Scurvy Scott wrote: I'm fairly new to python having recently completed LPTHW. While randomly reading stack overflow I've run into lambda but haven't seen an explanation of what that is, how it works, etc. Would anyone care to point me in the right direction? lambda is an alternative syntax for defining a function. However, the function has no name, and can be defined in the middle of another expression. The main constraint is that a lambda function can consist only of a single expression. No statements, no if's, no loops. It's main usefulness is for callbacks, for example for a sort operation, or gui event handlers. if you had a function that returned the square of its argument, you might define it as follows: def square(x): return x*x You could accomplish the same thing by the lambda function: square = lambda x : x*x Here we create a function with lambda, then bind it to the name square. No benefit, but it shows the syntax. More interesting is if we want to sort a list of two-tuples, using the 3rd element of each as our sort key. mylist.sort(key=lambda x : x[2]) There are other ways to accomplish that (and I think there's a standard library function for it), but maybe it shows you what it could be used for. The function is generated, the sort happens, and the function is discarded. (all code untested) -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 102, Issue 98
On Aug 30, 2012, at 8:15 PM, tutor-requ...@python.org wrote: Message: 6 Date: Fri, 31 Aug 2012 00:15:41 + From: Ashley Fowler afowl...@broncos.uncfsu.edu To: tutor@python.org tutor@python.org Subject: [Tutor] Printing a list as a column Message-ID: 6962c976ae76ac4298cbf6fd6d0c63561f37c...@bl2prd0710mb363.namprd07.prod.outlook.com Content-Type: text/plain; charset=iso-8859-1 Does anyone know how to print a list in a form of a column instead of a row? How about - for item in iter(list): ….print item -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lambda?? Whaaaaat?
On 31/08/12 13:39, Scurvy Scott wrote: I'm fairly new to python having recently completed LPTHW. While randomly reading stack overflow I've run into lambda but haven't seen an explanation of what that is, how it works, etc. Would anyone care to point me in the right direction? Lambda is just a short-cut for making a function. Instead of writing a function like this: def func(a, b=2): return a*b - 1 you can do this instead: lambda a, b=2: a*b - 1 Note that you don't use the return keyword in lambdas. There are two limitations on functions you create with lambda: * they don't have names -- they are anonymous functions (this is both a strength and a weakness) * you are limited to a single expression in the function body, so big complicated functions can't be (easily, or at all) be created with lambda Other than that, they are ordinary functions no different from those you create using def. Why would you use lambda instead of def? Frankly, normally you wouldn't, but there are a few situations where you might. One of the most common is using callback functions for GUI frameworks or similar. Here's another example, easier to show. Suppose you want to sort a list of movie titles, but skipping leading The. movies = [Star Wars, The Lord of the Rings, Zoolander, The Last Supper, True Lies, Watchmen, Con Air, The Frighteners, The Long Kiss Goodnight, The Avengers] from pprint import pprint movies.sort(key=lambda title: title.replace(The , )) pprint(movies) Without lambda, you would have to do this: def key_function(title): # I can't think of a better name return title.replace(The , ) movies.sort(key=key_function) which is a bit of a waste if you only use key_function once and never again, or if you have many different key functions. (Note that, strictly speaking, my key function above is not quite right. Here is a better one: lambda title: title[4:] if title.startswith(The ) else title but that's a bit harder to understand. So the strength of lambda is that it is an expression, not a statement, and can be embedded directly where you want it. Here's a list of functions: list_of_functions = [ lambda s: s.upper() + !!, lambda s: ?? + s.lower(), lambda s: ? + s.title() + !, ] for func in list_of_functions: print(func(Hello world)) Where does the name come from? Lambda is the Greek letter L, and for reasons I don't know, it is the traditional name used for functions in some of the more abstract areas of computer science. From computer science the name became well known in programming languages like Lisp, and from there to Python. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 102, Issue 98
I think the OP is about to get the point, that there are usually several workarounds that people have found on their way to designing a function, or using built-ins(not that we don't use built-ins to create those workarounds). But you have to always remember to reference the docs, google. the help() function, then the list, unless it's something urgent, like a client needing a function added quickly, and you haven't worked with python in that particular area yet. Best Regards, David Hutto, http://hitwebdevelopment.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor