3

I am new to python and have been shown 'drivers' for running functions without entering them into the command line,

I don`t understand the concept of drivers or how to type them out correctly, any kind of feedback on how to use them would be great!.

What I don't understand is how entering the function makeGreyscaleThenNegate(pic) can call the def function makeGreyscaleThenNegate(picture): when the input values are different (pic) versus (picture). (I guess this is because I don't know how the 'driver' function works.)

Here is what i've been shown

def driverGrey():
  pic=makePicture(pickAFile())
  repaint(pic)
  makeGreyscaleThenNegate(pic)
  repaint(pic)

def makeGreyscaleThenNegate(picture):
  for px in getPixels(picture):
    lum=(getRed(px)+getGreen(px)+getBlue(px)/3
    lum=255-lum
    setColor(px,makeColor(lum,lum,lum))

My belief is for this to work,(pic) would already have been named/defined prior to creating the 'driver' function? I just am not sure how (pic) and (picture) are referring to the same file, or am I completely mis-interpreting this..

Kimvais
  • 38,306
  • 16
  • 108
  • 142
jitterbug
  • 43
  • 1
  • 1
  • 9
  • "when the input values are different (pic) versus (picture)" The NAMES are different, but that doesn't matter. – LostAvatar Sep 27 '13 at 07:06
  • 1
    "Driver" is not a concept in Python, or indeed a programming concept at all. This is just a question about functions. – Daniel Roseman Sep 27 '13 at 07:57
  • Dan, I see, sorry my instructor said they were called drivers because they executed a test of the function without entering it into the command line, thats what confused me as well. thanks for correctin.. – jitterbug Sep 27 '13 at 12:03
  • this is in JES by the way, Jython, sorry I forgot to mention that. – jitterbug Sep 27 '13 at 12:30

2 Answers2

1

This is really CS101 and nothing Python-specific actually. A function is a name for a code snippet. The names of the variables you pass as arguments to the function and the names of the arguments in the function are totally unrelated, they are just names. What happens in the above snippet:

def driverGrey():
    pic=makePicture(pickAFile())
    # 1. call a function named 'pickAFile' which I assume returne a file or filename
    # 2. pass the file or filename to the function named 'makePicture' which obviously
    #    returns a 'picture' object (for whatever definition of a 'picture object')
    # 3. binds the 'picture object' to the local name 'pic'

    (snip...)

    makeGreyscaleThenNegate(pic)
    # 1. pass the picture object to the function named 'makeGreyscaleThenNegate'.
    #
    #    At that time we enter the body of the 'makeGreyscaleThenNegate' function, 
    #    in which the object known as 'pic' here will be bound to the local 
    #    name 'picture' - IOW at that point we have two names ('pic' and 'picture')
    #    in two different namespaces ('driverGrey' local namespace and
    #    'makeGreyscaleThenNegate' local namespace) referencing the same object.
    #
    # 2. 'makeGreyscaleThenNegate' modifies the object. 
    #
    # 3. when 'makeGreyscaleThenNegate' returns, it's local namespace is destroyed
    #    so we only have the local 'pic' name referencing the picture object,
    #    and the control flow comes back here.

    (snip...)
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • right!, so at point 3 the functoin to negate ends, so the name association ends locally, and we go to the driverGrey(): to the last step in the body, repaint(pic), thankyou kindly for breaking down the steps sequentially. – jitterbug Sep 27 '13 at 13:19
0

pic and picture are just names or labels. You can call a chunk of data whatever you want. For example, a Spaniard might call a bottle of milk "leche" whereas a Frenchman might call it "lait".

The same thing applies to Python. You have some sort of "picture" object, and throughout your program, you're calling it by different names. In the driverGray function, you call it pic, and in the makeGrayscaleThenNegate function, you call it picture. Different name, same object.

If I were to do this:

pic = makePicture(pickAFile())
b = pic
c = b

...both pic, b, and c are all referring to the exact same "thing". If I make a change to b by doing something like b.var = 13, both c and pic will change too.

(Note: if you did something like c = 1, then you're saying that c now means a number, rather then a picture object. The pic and b variables are unaffected.

Here's a metaphor: if somebody were to poison the milk, then it doesn't matter what the Spaniard or the Frenchman calls the milk -- it's poisoned, regardless of the particular name.


In your case, when you do makeGreyscaleThenNegate(pic) inside the first function, you are saying that you "pass in" a picture object (which you happen to call pic). The makeGrayscaleThenNegate function is defined as def makeGreyscaleThenNegate(picture):. This means that the first argument that is passed in will be called "picture" for the duration of that function.

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224