0

I am very new to python, and am writing a code with multiple modules.

Each of my modules manipulates the same list that I have named "myList", however, I am having trouble understanding how to get the list into each module.

For example, one module called reverseList.py needs to take the list and reverse it, but when it is run in my main function, I get an error that the list does not exist.

Import reverseList

def main():

with open('list3.txt', 'r') as f:
data = f.read()
data = data.split("\n")
myList = [row.split(",") for row in data]

reverseList.reverse()

NameError: name 'myList' is not defined

The reverseList.py module is as follows:

def reverse()
    myList1 = myList[::-1]
    print(myList1)
    return
  • 1
    There are some problems with your code: mode 'r' in open function should be quoted; current indentation leads to IndentationError. Also it is unclear what on what list reverseList.reverse() will work. – Yuri Ginsburg Jul 23 '19 at 22:00
  • 1
    It sounds like you have a variable inside `reverseList` with the same name. However, python doesn't pull variables between scopes implicitly. We don't have enough detail about `reverseList` to be more specific, however – G. Anderson Jul 23 '19 at 22:02
  • As an aside, python [naming conventions](https://www.python.org/dev/peps/pep-0008/#naming-conventions) are for classes to be `CapitalizedWords`, and for functions and variables to be `lowercase` or `snake_case` , not `camelCase` – G. Anderson Jul 23 '19 at 22:05
  • Hi! Thanks all for the help - the errors in the code presented are more or less a result of copying error. The code works and is indented correctly. The problem I am having however, is that the .txt file that I have created a list from is not being picked up when I import my module. I am pretty sure I am missing something key with this.. In the module reverseList I am trying to manipulate "myList" yet it is not being picked up. I hope this helps clarify the issue, all coding errors aside – beginnerlearner456 Jul 23 '19 at 22:08
  • @G.Anderson may you please take another look – beginnerlearner456 Jul 23 '19 at 22:55
  • @YuriGinsburg may you please take another look – beginnerlearner456 Jul 23 '19 at 22:55
  • 1
    Leaving aside the rest of problems with indentation. MyList variable defined in module main is unknown to reverse function from module reverseList You should have list as an argument in function reverse. P. S. Global variables are evil. – Yuri Ginsburg Jul 23 '19 at 23:54
  • [Basic explanation of scoping rules](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) – G. Anderson Jul 24 '19 at 15:06

1 Answers1

2

It is unclear where exactly the error comes out since you didn't include your entire main function - there is no place myList is used before/after its definition. But I can think of a couple of reasons for the error.

  1. The error shows up when a variable is used before it is defined. So, you might have the variable myList before you define it in line 4 of your current code.

  2. In python, indentation is the matter of correctness of your code. Meaning, line 2~4 should be indented if you didn't do so.

I might be able to come up with a better answer if you present the rest part of the code.


Your update reveals where the problem is happening; that is from your reverseList, not your main.

You are loading myList in reverseList, but it has never been defined. myList defined in main is not accessible from reverseList. To fix this problem, you should pass myList as an argument to the function reverse when it is called and use that in your reverse function.

main: reverseList.reverse(myList)

reverseList:

def reverse(myList):
    myList1 = myList[::-1]
    print(myList1)
    return

Note that the argument name of the function reverse can be different from the one in main. In other words, it doesn't matter if you do follow;

def reverse(myList2):
    myList1 = myList2[::-1]
    print(myList1)
    return

This might be better:

def reverse(myList2):
    print(myList2[::-1])
Jin
  • 304
  • 2
  • 10