Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Question Number 1 (http://www.programmingforums.org/showthread.php?t=14573)

blake_jl Nov 24th, 2007 12:40 AM

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():
    print
def threeLines():
    newLine()
    newLine()
    newLine()
def nineLines():
    threeLines()
    threeLines()
    threeLines()
def twentysevenLines():
    nineLines()
    nineLines()
    nineLines()
print "First Line."
twentysevenLines()
print "Second Line."


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."
twentysevenLines()
print "Second Line."
def newLine():
    print
def threeLines():
    newLine()
    newLine()
    newLine()
def nineLines():
    threeLines()
    threeLines()
    threeLines()
def twentysevenLines():
    nineLines()
    nineLines()
    nineLines()


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?

DaWei Nov 24th, 2007 12:55 AM

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.

blake_jl Nov 24th, 2007 1:09 AM

Re: Question Number 1
 
Quote:

Originally Posted by DaWei (Post 137460)
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.

So why would this book (How to think like a computer scientist, Learning with Python) give me this excercise? The way the excercise is written I'm expecting an error. It had another one after where I put the def newLine() after the def threeLines() but the same thing happened. It worked perfectly.

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.

DaWei Nov 24th, 2007 1:54 AM

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.

blake_jl Nov 24th, 2007 2:17 AM

Re: Question Number 1
 
Quote:

Originally Posted by DaWei (Post 137462)
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.

Ill stop where I am in the book and take your advice. The book I'm reading did mention this topic at the start. Maybe I need to read that part again but for some reason or another I thought that because we were both using the same program (Python) everything else would also be the same. Eg. my interpreter understands the entire script before its asked to execute any part of it, so why doesn't the authors?

Google might help me.

Thanks again.

titaniumdecoy Nov 24th, 2007 3:15 AM

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.

Sane Nov 24th, 2007 8:08 AM

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:
  • A function's contents are defined with a def block.
  • The contents are called with the appropriate function name when you need the function to execute.
  • The logical ordering of these two events is A, B. So it's conventional to keep them ordered that way in your code.

When the interpreter already does that for you, it merely becomes a coding convention.

DaWei Nov 24th, 2007 12:01 PM

Re: Question Number 1
 
I think the key is in the OP's second post:
Quote:

when Python first loads up and I imported the script.
In BoaConstructor, this fails:
UnboundLocalError: local variable 'twentysevenLines' referenced before assignment
:

modules = {}

def main ():
    print "First Line."
    twentysevenLines()
    print "Second Line."

def newLine():
    print
def threeLines():
    newLine()
    newLine()
    newLine()
def nineLines():
    threeLines()
    threeLines()
    threeLines()
def twentysevenLines():
    nineLines()
    nineLines()
    nineLines()

if __name__ == '__main__':
    main()

but this works:
:

from precmod import *
modules ={'precmod': [0, '', 'precmod.py']}

def main ():
    print "First Line."
    twentysevenLines()
    print "Second Line."

if __name__ == '__main__':
    main()

precmod.py
:

def newLine():
    print
def threeLines():
    newLine()
    newLine()
    newLine()
def nineLines():
    threeLines()
    threeLines()
    threeLines()
def twentysevenLines():
    nineLines()
    nineLines()
    nineLines()


blake_jl Nov 24th, 2007 4:59 PM

Re: Question Number 1
 
I saved
:

print "First Line."
twentysevenLines()
print "Second Line."
def newLine():
    print
def threeLines():
    newLine()
    newLine()
    newLine()
def nineLines():
    threeLines()
    threeLines()
    threeLines()
def twentysevenLines():
    nineLines()
    nineLines()
    nineLines()


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.

titaniumdecoy Nov 24th, 2007 5:13 PM

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