Are rank noobs tolerated, here?

hdante hdante at gmail.com
Tue May 6 13:06:28 EDT 2008


On May 5, 3:43 pm, notbob <not... at nothome.com> wrote:
>
> ----------
>
> "Here is an example of a user-defined function that has a parameter:
>
> def print_twice(bruce):
>     print bruce, bruce
>
>
> ****ME****
> is this just an example of how the def should be written and it doesn't
> really do anthing... yet?  I read another newb explanation here:http://www.codepedia.com

 That's right. Definitions only explain how things work. They are used
later.


> and give the command print myfirstfuntion and I get back this:
> <function myfirstfunction at 0xb7b9c994>

 When you have a problem with your code, always post here the complete
code and a complete execution. For example:

 file myfile.py:
 --------------------------
 def myfirstfunction():
      abcde

 myfirstfunction()
 --------------------------

 execution:
 $ python myfile.py
 Traceback (most recent call last):
   File "myfile.py", line 4, in <module>
     myfirstfunction()
   File "myfile.py", line 2, in myfirstfunction
     print abcde
 NameError: global name 'abcde' is not defined
 ------------------------------------------------

 This way we can find out what's wrong (don't worry about the code
above, it's just an example)

> ok, I try and follow the above, but where is he getting the script?  So, I
> make a script called chap03.py and put it in ~/pyth/chap03/.

 "import" is very complex. For example, in my system, it looks for
files in all these places:

['', '/usr/bin', '/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/
lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/
python2.5/lib-dynload', '/usr/local/lib/python2.5/site-packages', '/
usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/
Numeric', '/usr/lib/python2.5/site-packages/PIL', '/usr/lib/python2.5/
site-packages/gst-0.10', '/var/lib/python-support/python2.5', '/usr/
lib/python2.5/site-packages/gtk-2.0', '/var/lib/python-support/
python2.5/gtk-2.0', '/usr/lib/python2.5/site-packages/wx-2.8-gtk2-
unicode', '/var/lib/python-support/python2.5/IPython/Extensions', '/
home/hdante/.ipython']

 Those are directories that it uses to find files. You tell import
just the base name of the script "chap03". Then it adds the ".py"
extension, looks in each directory in order, etc. Notice in the
directory list that the first entry is a blank: ''. It represents the
current directory. So, if you do:

 $ cd ~/pyth/chap03
 $ python
 >>> import chap03

 It will work as expected.

>
> In a function call, the value of the argument is assigned to the
> corresponding parameter in the function definition.  In effect, it is if
> bruce = 'Spam' is executed when print_twice('Spam')
> is called, bruce = 5 in print_twice(5), and
> bruce = 3.14159 in print_twice(3.14159)."
>
> ****ME****
> OK, I'm totally lost.  Neither the above or the other link works or make any
> sense at all.  Is the above print_twice(5), etc, supposed to work like the
> original print_twice(bruce) function (which still doesn't!), just with a

 Words that appear in a program, like, "bruce" are variables.
Variables are stamps. Bruce is a stamp. Here is bruce:

 /----------\
 |  bruce   |
 \----------/

 You can put the stamp in other things:


 /----------\  glued
 |  bruce   | ------> 42
 \----------/

 When you use a stamp, it returns what it's glued on:

 >>> bruce = 42  # glue a stamp
 >>> print bruce # use the stamp
 42

 When you call print_twice(42), first bruce glued to the value 42.
When you use bruce:

 print bruce, bruce

 The value 42 is retrieved and given to the "print" function.

 So, I can glue arbitrary things with bruce:

 >>> print_twice(5)
 5 5

 >>> print_twice(3.14)
 3.14 3.14

 etc.

 If you want to print "bruce bruce", then you need a special kind of
value, called a string:

 >>> print_twice('bruce')
 bruce bruce

 'bruce' is a string (notice the quotes). In this case bruce is glued
to 'bruce':

 /----------\  glued
 |  bruce   | ------> 'bruce'
 \----------/

 When you pass strings to "print", they appear the way they are
written.

 >>> print 'bruce'
 bruce
 >>> print_twice('bruce')
 bruce bruce


> different parameter?  Call me stupid, but I'd sure like to get past this.

 Stupid. Not really. :-) I'd rather say that the problem is the book.
The book is just another introduction to programming. There's no
special attempt to make the reader learn to think like a computer
scientist.

> How am I supposed to get the original def to work?  I've yet to figure out

 file myfile.py:
-------------------------------
def print_twice(bruce):
	print bruce, bruce

print_twice('bruce')
print_twice(2)
-------------------------------

execution:
-------------------------------
$ python myfile.py
bruce bruce
2 2


 Finally, you don't understand something, ask again.



More information about the Python-list mailing list