0

I have written a small scrip (this is partial), the full code should search a bunch of .c files and check if the parameters within it are being used or not. This particular code is responsible for grabbing the parameter from a row, so it can be used to search the .c files for identical parameter names and it's values.

The issue is that the first instant of print (inside takeTheParam method) shows the correct parameter in command prompt, while the second print instant (after the call to the takeTheParam method) shows a blank in command prompt.

import os

theParam = ""

def takeTheParam(row, theParam):
    for item in row.split():
        if "_" in item:
            theParam = item
            print theParam
            return theParam


for root, dirs, files in os.walk('C:/pathtoworkdir'):
    for cFile in files:
        if cFile.endswith('.c'):
            with open(os.path.join(root, cFile), 'r') as this:
                for row in this:
                    if '=' in row:
                        takeTheParam(row, theParam)
                        print theParam
                        while theParam not in usedParameters:  # Has the param already been checked?
                            value(row, savedValue, statements, cur)
                            searchAndValueExtract(theParam, parameterCounter, compareValue)
                            while isEqual(savedValue, compareValue, equalValueCounter):
                                searchAndValueExtract(theParam, parameterCounter, compareValue)
                            else:
                                # If IsEqual returns false, that means a param has different values 
                                # and it's therefore being used
                                usedParameters.append(theParam)
                                pass

I have't got enough experience in python to figure out why this happens, but I suspect that when theParam is used outside of the method it's value it retrieved from it's definition on the beginning of the code (theParam = "") and I have no idea why, if this is the case.

this.guy
  • 91
  • 9
  • 1
    The similar name of the variable and the one used in the function declaration is fooling you. See http://stackoverflow.com/a/292502/2564301 – Jongware Apr 29 '16 at 08:27

1 Answers1

3

Change

 takeTheParam(row, theParam)

to

theParam = takeTheParam(row, theParam)

The returned variable is never assigned to theParam in your case, so it would stay "" forever. Now it isn't anymore.

Mathias711
  • 6,568
  • 4
  • 41
  • 58
  • It's working for `takeTheParam` but what should I do if one of my methods return two values? – this.guy Apr 29 '16 at 08:31
  • then you would do `param1, param2 = func_that_returns_two_variables()`. And return within that function with `return param1, param2` – Mathias711 Apr 29 '16 at 08:35