0

So I have 2 files , file1.py and file2.py

I have a couple of lists sam and dan from file2.py

so when I try this from file1.py I get

AttributeError: 'module' object has no attribute 'myvar'

file1.py

import file2

f_var=[]
myvar = raw_input("some string")

if myvar == "sam":
    f_var = loopbov("sam")


def loopbov(myvar):
    lst=[]
    for each in file2.myvar:
        te = fet(each):
        lst.append(te)
    return lst

def fet(vpf):
    tmp=[]
    # this gets me an list as an output
    return tmp

file2.py

sam=["33:89","21:70"]
dan=["34:43","23:56"]

I know it's a simple error but can someone explain how to access by parsing a object here to access from another file

4
  • 1
    file2 doesn't have a myvar field. It has sam and dan. Give it a try to for each in getattr(file2, myvar): (and check this about getattr ) Commented Jan 4, 2018 at 21:51
  • #1, show how you are running the code. #2, what line are you getting the error on? Commented Jan 4, 2018 at 21:53
  • 1
    @BorrajaX. Sorry about the last comment. Deleted it. You should draft an answer explaining why and how. Commented Jan 4, 2018 at 21:57
  • @BorrajaX It worked,thanks! Commented Jan 4, 2018 at 21:58

2 Answers 2

2

So, in file2.py there are two variables defined, right? sam and dan.

You can see that taking advantage than in Python, everything is an object:

import file2
print("This is what I found in file2:\n%s" % vars(file2))

You'll see a looot of stuff, but among other things, you will see your sam and dan variables. There is no myvar name, right? In your question file1.py's myvar will contain the name of the variable that you want to access...

And here's where getattr comes in: Given an instance, how can you get one of its fields if you know its name (if you have the name stored in a string). Like this: getattr(file2, myvar)

So your loopbov function, where you pass the name of the list to iterate in the myvar argument would become:

def loopbov(myvar):
    lst=[]
    for each in getattr(file2, myvar):
        # myvar will be the string `"sam"`
        te = fet(each):
        lst.append(te)
    return lst

As @MadPhysicist mentioned in his comment, it's probably worth mentioning what getattr will do when it's trying to get an attribute by name that is not defined: You will get an AttributeError exception. Try it. Put somewhere in your file1.py this code: getattr(file2, "foobar") However! If you see the docs for getattr you will see that accepts an optional argument: default. That can be read like "ok, if I don't find the attribute, I won't give you an error, but that default argument".

Probably it's also worth it reading a bit, if you're interested in the subject, about how import works and the namespaces it creates (see this and this)

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry I accidentally clobbered your edits for a second there.
Haha, no worries!! I tend to edit over and over and over. Appreciate the edits!
You might want to mention the optional extra argument to getattr, or the possibility of an AttributeError in the case where the user enters something besides 'sam' or 'dan'.
Got that covered guys, it's part of a larger code was just stuck here and 'Sam' and 'Dan' are actually a list gathered out of json response ;)
0

In file2.py use a dictionary:

my_vars = {'sam': ["33:89","21:70"], 
           'dan': ["34:43","23:56"]}

Now, in file1.py:

import file2

def loopbov(myvar):
    lst=[]
    for each in file2.my_vars[myvar]:
        te = fet(each)
        lst.append(te)
    return lst

def fet(vpf):
    tmp=[]
    # this gets me an list as an output
    return tmp


f_var = loopbov("sam")

3 Comments

The module is already a dictionary to all intents and purposes. Why the overkill?
Feels cleaner to use a dict instead of working with 'getattror vars`.
Thanks will keep it in mind , but getattr served my purpose here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.