Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 23rd, 2007, 11:40 PM   #1
blake_jl
Newbie
 
Join Date: Nov 2007
Posts: 14
Rep Power: 0 blake_jl is on a distinguished road
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?
blake_jl is offline   Reply With Quote
Old Nov 23rd, 2007, 11:55 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 24th, 2007, 12:09 AM   #3
blake_jl
Newbie
 
Join Date: Nov 2007
Posts: 14
Rep Power: 0 blake_jl is on a distinguished road
Re: Question Number 1

Quote:
Originally Posted by DaWei View Post
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.
blake_jl is offline   Reply With Quote
Old Nov 24th, 2007, 12:54 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 24th, 2007, 1:17 AM   #5
blake_jl
Newbie
 
Join Date: Nov 2007
Posts: 14
Rep Power: 0 blake_jl is on a distinguished road
Re: Question Number 1

Quote:
Originally Posted by DaWei View Post
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.
blake_jl is offline   Reply With Quote
Old Nov 24th, 2007, 2:15 AM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 841
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Nov 24th, 2007, 7:08 AM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,826
Rep Power: 5 Sane will become famous soon enough
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.
Sane is online now   Reply With Quote
Old Nov 24th, 2007, 11:01 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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()
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 24th, 2007, 3:59 PM   #9
blake_jl
Newbie
 
Join Date: Nov 2007
Posts: 14
Rep Power: 0 blake_jl is on a distinguished road
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.
blake_jl is offline   Reply With Quote
Old Nov 24th, 2007, 4:13 PM   #10
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 841
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
c-unix-childprocesses-random number programmingnoob C 7 Feb 6th, 2007 8:39 PM
very simple question. make button change number in edit box nickm Delphi 2 Apr 29th, 2006 10:47 PM
One more question infinite! massive-war C++ 8 Apr 11th, 2005 12:11 AM
non repeating random number generation gencor45 C# 2 Feb 9th, 2005 12:11 AM
Number Systems... Ade Coder's Corner Lounge 7 Jan 19th, 2005 5:32 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC