![]() |
Question Number 1
Hi All,
I am new. I have posted in the introduction forums. You can read my post there to get to know why I am here. As it says, I am a newbie. Teaching myself (with your help) Python. This is my first question, very basic but I'm starting right from the beginning. I am reading through a beginners book that was linked to another thread in this forum. It got me to create the following script :
def newLine():No dramas at all. It just prints the first line, then 27 blank lines and then the second line. As an excercise the book gets me to: "move the last three lines of this program to the top, so the function calls appear before the definitions. Run the program and see what error message you get." This should be like this right? :
print "First Line."The only problem is I dont get any errors. It prints the first line, then 27 blank lines and then the second line just like the original script. What am I missing here? Should there be an error? |
Re: Question Number 1
That works because it's making a pass through the code to pick up the definitions and then a pass to execute. Not all languages or assemblers or compilers or interpreters give you that facility. It's fairly common for interpreters, though. They have to emit the actual runnable code, so they often generate all of it, then run it.
|
Re: Question Number 1
Quote:
Sorry to waste your time with such simple questions, but I want to be thorough and learn this properly. By the way, the book doesn't tell me how to use the program. I had to figure that out myself. Am I doing this right? I create a new script for the above example, then go into the interactive window that is there when Python first loads up and I imported the script. |
Re: Question Number 1
Suppose I read your second script to you one line at a time. When I said, "Print 27 lines" you wouldn't know how to do it, because you don't know how to print even one line.
On the other hand, if I let you read the script in advance, you know how to print one line, three lines, nine lines, and twenty-seven lines. It's that simple. If your interpreter sees and understands the entire script before it's asked to execute any part of it, you're home free. If it doesn't, you aren't. It isn't magic; it's implementation. You might want to read some basic "How it works" stuff for computers and computer languages and how they get the job done in a few but various ways. |
Re: Question Number 1
Quote:
Google might help me. Thanks again. |
Re: Question Number 1
The phrase "run the program and see what error message you get" does not necessarily imply that an error message will occur. You are probably not missing anything; the author simply wanted you to realize that you would get an error in most other languages, but not Python.
Another possibility, though unlikely, is that the book you are reading was written a very long time ago and the author was using an older version of Python that behaved differently. The Python language is constantly evolving. |
Re: Question Number 1
I assume you're using IDLE? I do get the error with IDLE in Python 2.5.
I'm thinking your interpreter cached the functions when you were writing and running it. Then, when you moved the bottom three lines to the top, it had already known where the functions are because they were cached by the precompiler. Try saving the finalized script as a new file, then run that. You should see the error. If not, you simply have a different implementation of Python, or one that behaves differently on a different machine. For all we know, mine and the author's Python could be the ones behaving "incorrectly". Nevertheless... don't worry about it. The main point of that section in the tutorial was to reinforce the notion:
When the interpreter already does that for you, it merely becomes a coding convention. |
Re: Question Number 1
I think the key is in the OP's second post:
Quote:
UnboundLocalError: local variable 'twentysevenLines' referenced before assignment :
modules = {}:
from precmod import *:
def newLine(): |
Re: Question Number 1
I saved
:
print "First Line."as a new script, and then this time I clicked run instead of import. It still worked. Although I do understand the order in the way things work, I don't understand the terminology you are using and I don't understand what goes on in the background to make it work. My understanding in plain English: 1. Show it how to do something 2. Tell it to do it I'm having no problems with this. I guess in a way CSS is similar. You write all the rules in the CSS document, link to it in your header and then call on those rules in your page. When I looked at this exercise, the first thing I thought (once again in plain english) was "Ok, im telling it what to do before it knows how to do it, I wonder what the error will be". I did the exercise and there was no error. My problem is I don't know why there was no error. I need to take the previous advice and learn about how things work. I tried Googling a few terms but haven't had much success. It's crazy. Here I am having problems with not having an error. It should be the opposite. |
Re: Question Number 1
There is no reason to spend so much time obsessing about not getting an error message. You are making a mountain out of a molehill. Move on already.
|
| All times are GMT -5. The time now is 3:27 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC