Instead of me posting a bunch of different threads (because I am sure there will be multiple questions) I figured I would just post everything here. Thanks for being patient with me guys!

So I am working my way through learnpythonthehardway.org and am up to lesson 20. I understand everything so far, but only because my father in law (who has been a programmer forever) has helped me along the way. I just don't always want to bug him

what does the 'dot' in f.seek(0) mean? is it just specifying which file you want to 'seek' in so python knows? shouldn't it just know what to 'seek' from the line above (since the f.seek is below and indented under where you define the rewind(f) function).

also... couldn't I just use this code below instead? what is the purpose of labeling it as 'f'?

in this same example (lesson 20), i don't think i am fully understanding the open() function. is it right to say 'open(input_file)' is returning a file handle (i.e. returns a certain integer spot) in the RAM? can someone explain more fully in laymen's terms? thanks

You need to understand the concept of namespaces which throw a lot of people but it's really just a simple naming convention. Think of a namespace as a container used to differentiate similar objects. In my company there are several men named Bob. I can differentiate all the Bobs without even knowing their last name by calling them Bob from Accounting, Bob from Sales, etc. In this example Accounting and Sales are namespaces. This concept can be expanded. If we have Eastern and Western divisions in my company I can identify a particular Bob by saying "The Bob from Sales in the Western Division or Western.Sales.Bob! In Python pretty much everything is an object and you can access the "stuff" inside it with dot notation.

my_string = "hello world" # Poof, a new string object is created with all the power of python strings.print my_string.title # Capitalizes Every Wordprint my_string.index('w') # Returns index of the "w" in "world"

Play around with this. Even with IDLE, once you create an object (like my-string) you can type the dot (period) and if you wait a second, IDLE will open a pop-up with all the attributes and methods of that object type for you to select. Try it...

in this same example (lesson 20), i don't think i am fully understanding the open() function. is it right to say 'open(input_file)' is returning a file handle (i.e. returns a certain integer spot) in the RAM? can someone explain more fully in laymen's terms? thanks

Kind of. The take away point though is that all that tricky hardware based stuff like RAM address and file IO are abstracted away so you don't need to know about it. Instead you have a FILE OBJECT that has it's own attributes and methods which you can conveniently use to do FILE-LIKE things such as read, seek, flush, etc.

shouldn't it just know what to 'seek' from the line above (since the f.seek is below and indented under where you define the rewind(f) function)

It's possible the python parser(i.e "code reader") could be programmed to try and deduce that, but it's much simpler to write a parser that calls seek() on whatever is specified to its left.

what does the 'dot' in f.seek(0) mean?

You've probably been dealing with functions and variables so far, but there are also things called classes. You'll learn about those later. For now, just accept that you have to use that syntax to do stuff to the file you opened.

Marbelous wrote:The idea here is that you're passing in an object (called 'f') to the rewind function that already has a method called seek, then calling that seek method within the function called rewind.

You need to understand the concept of namespaces which throw a lot of people but it's really just a simple naming convention. Think of a namespace as a container used to differentiate similar objects. In my company there are several men named Bob. I can differentiate all the Bobs without even knowing their last name by calling them Bob from Accounting, Bob from Sales, etc. In this example Accounting and Sales are namespaces. This concept can be expanded. If we have Eastern and Western divisions in my company I can identify a particular Bob by saying "The Bob from Sales in the Western Division or Western.Sales.Bob! In Python pretty much everything is an object and you can access the "stuff" inside it with dot notation.

my_string = "hello world" # Poof, a new string object is created with all the power of python strings.print my_string.title # Capitalizes Every Wordprint my_string.index('w') # Returns index of the "w" in "world"

Play around with this. Even with IDLE, once you create an object (like my-string) you can type the dot (period) and if you wait a second, IDLE will open a pop-up with all the attributes and methods of that object type for you to select. Try it...

That helps a lot! Thanks! A few questions about your points...

in the 'Western.Sales.Bob' example, western and sales would be namespaces but bob would be the method? is the last thing in the 'dot string' (my made up word) always the method? just wanting to clarify.

how come python, for the first one (age = add(30, 5)) it returns the string under that defined function at the top. but for the second one (print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)) it returns an intiger (i.e. the 'return' part under the SAME defined function. Shouldn't they return the same thing?

Python replaces all "function calls" in your code with the return value of the function. A "function call" is a name with some parentheses after it, e.g. add(1, 5). Whenever python sees a function call in your code, execution stops at the point of the function call, execution jumps to the function and python executes the function, then execution jumps back to the point where execution was previously halted and python substitutes the function call with the return value of the function; then execution continues.

A rule of thumb when you are substituting into a string: ALWAYS use %s unless that doesn't do what you want. If you don't get the formatting you want with %s, then you can try %d, %f, etc.

(print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)) it returns an intiger (i.e. the 'return' part under the SAME defined function. Shouldn't they return the same thing?

The function name is add(). add() is not called in that print statement, so it is irrelevant what add() does or doesn't do. All that matters are the values assigned to the variables age, height, weight, and iq.

Last edited by 7stud on Fri May 16, 2014 6:14 pm, edited 1 time in total.

josh623 wrote:That helps a lot! Thanks! A few questions about your points...

in the 'Western.Sales.Bob' example, western and sales would be namespaces but bob would be the method? is the last thing in the 'dot string' (my made up word) always the method? just wanting to clarify.

also, what is IDLE?

No, not always. In this case, most likely "Bob" would be an instance of a class with his own methods and attributes that can be accessed by typing another DOT. I would suggest you don't go too far into OOP concepts yet. Stick with learning how variable, data structures and functions work. When you get to OOP you'll see that variables and structures are now called "attributes" and functions are called "methods" but they're really the same thing made even more useful since they apply to classes and the instances made from those classes. OOP is something you eventually "Grok" by working with it until the light bulb comes on in your head. Just give it time...

IDLE is the Integrated Development Environment (IDE) that comes included with most python distributions. It's used to write, run and de-bug python code. You don't need to use an IDE. You can always just write code in a text editor (Notepad, etc.) and run it from the command line. Once you get comfortable with python syntax though, you'll find a good IDE can be a big help. NOTE: IDLE in not very good but it's simple and almost always available so it's not a bad starter IDE. Since you're using LPTHW I assume they want you to avoid the shortcuts an IDE provides so you can focus on core python. BTW, it's named after Eric Idle, one of the Monty Python guys.

so when you assign a function to a variable (i.e. age = add(30, 5)) then initially when you do the "assigning" (forgive my incorrect terminology!) then it will print the string. But then whenever you call the function it will return a value?

Python replaces all "function calls" in your code with the return value of the function. A "function call" is a name with some parentheses after it, e.g. add(1, 5). Whenever python sees a function call in your code, execution stops at the point of the function call, execution jumps to the function and python executes the function, then execution jumps back to the point where execution was previously halted and python substitutes the function call with the return value of the function; then execution continues.

is the add(30, 5) not a function call? I am just confused as to why this doesn't return a value and returns a string initially, but later when you refer to the age it returns the value. it seems to me like the age is supposed to equal or be the same thing as the value, just like when you assign variables... sorry, I'm just confused!

(print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)) it returns an intiger (i.e. the 'return' part under the SAME defined function. Shouldn't they return the same thing?

The function name is add(). add() is not called in that print statement, so it is irrelevant what add() does or doesn't do. All that matters are the values assigned to the variables age, height, weight, and iq.

this is what I am confused about... it seems to me that add() is ultimately called because it uses 'age' which refers up a couple lines to the function add()... but I don't think I am thinking about this correctly... age should just refer to the VALUE of the called function add(), is this more correct?

Marbelous wrote:IDLE is the Integrated Development Environment (IDE) that comes included with most python distributions. It's used to write, run and de-bug python code. You don't need to use an IDE. You can always just write code in a text editor (Notepad, etc.) and run it from the command line. Once you get comfortable with python syntax though, you'll find a good IDE can be a big help. NOTE: IDLE in not very good but it's simple and almost always available so it's not a bad starter IDE. Since you're using LPTHW I assume they want you to avoid the shortcuts an IDE provides so you can focus on core python. BTW, it's named after Eric Idle, one of the Monty Python guys.

Thanks for the tip on slowing down and not diving in too deep... that is the hard part, to focus on what i need to be focused on and the rest will come.

So right now I am using a Linux Mint OS right now with bash as my command prompt and gedit as my text editor... seems to be working well! Yeah I definitely need to learn the basics before graduating to something that gives you shortcuts =)

then initially when you do the "assigning" (forgive my incorrect terminology!) then it will print the string.

The assignment does not cause the function to print the string, it's the function call that causes the function to print the string. If you put "(...)" after a function name, it tells python to execute the function. Try this:

They will both print the string. But only the second one assigns the return value of add() to the age variable.

You have to explain why it is that when you see a string printed to your screen, you think that is the return value of the function...when the rule is: the return value of a function is the value specified in the function's return statement. Here is a quiz, identify the return value of the following function:

One you can identify the return value of a function, then you know that python will replace the function call in your code with that return value.

age should just refer to the VALUE of the called function add(), is this more correct?

age will refer to the return value of the function add().

It's really as simple as printing out your code, crossing out every function call and replacing it with the return value of the function, and then trying to determine what the resulting code does. Try it with the following code, and post the code after you have replaced every function call with the function's return value:

but when you refer to 'work' later in the program then the return value would be 'goodbye', correct?

It's really as simple as printing out your code, crossing out every function call and replacing it with the return value of the function, and then trying to determine what the resulting code does. Try it with the following code, and post the code after you have replaced every function call with the function's return value:

One you can identify the return value of a function, then you know that python will replace the function call in your code with that return value.

def dance(): print "Tappity tap tap tap" return add(1, 1)# since we have not assigned the add() function to a variable yet all it knows to do is to 'print' when this function is called. and it will print '201' and 'hello'

a = add(10, 20)# here you assign the function add() with the perimeters of '10' and '20' as being variables within this function. So in the program when executed is this will not actually give me a return value yet, but will look like:[code]210hello[/code]

do_something()# we are getting into things a little bit above my head here, but I will take a wild guess. Shouldn't your original defined function 'do_something()' have a 'do_something(f)' with an f in it? I don't understand how it can still work and be blank? Anyway, whatever file is passed to this function it will open it with writable permissions, write "Today is a fine day." in the file, and then close it.

b = speak('woof')# you are assigning the function speak('woof') to the variable 'b' so that whenever 'b' is called the return value would be '100'

print a+b# now is when we get into getting the return values. the return value for 'a' is '10' and the return value for 'b' is '100' so Python will actually print '110', correct?

x = dance()print x# Python will print the return value of dance() which we actually have a function add(1, 1) within a function dance(). The return value that will be printed will be '10'