Question

i'm trying to make something in Python. I'm trying to import a variable from another file but it doesn't appear to work... as it gives this

   AttributeError: 'function' object has no attribute 'langName'

Here's the snippet which contains the variable (class is L10N - PS: not in the same code)

def buildName(self):
   langName = 'names_'+self.language.upper()+'.txt'
   print 'Current Loaded Names:'+langName()+'.'
   return langName

and the part where i try to import (this is where the error is):

names = [l.strip('\n\r').split('*')[1:] for l in open(pp+'data/etc/'+l10n.buildName.langName+'',"r").readlines() if not l.startswith('#')]

Anyways to fix it? I imported it and i'm not sure it would work.

EDIT: TypeError: unbound method buildName() must be called with l10n instance as first argument (got nothing instead)

Now gives this. I don't know why.

No correct solution

OTHER TIPS

You have two problems with the code. The first is in buildName, and I've commented it:

def buildName(self):
    langName = 'names_'+self.language.upper()+'.txt'
    print 'Current Loaded Names:'+langName+'.'   # <-- removed parens
    return langName  # <-- this is returned. no need to try to access outside the func

The second is in the call to that. l10n.buildName needs to be called, which returns langName for you, which doesn't need to be looked up.

names = [l.strip('\n\r').split('*')[1:] for l in open(pp+'data/etc/'+l10n.buildName()+'',"r").readlines() if not l.startswith('#')]

In your snippet

langName is a name, so you can't use () which is to invoke function

def buildName(self):
   langName = 'names_'+self.language.upper()+'.txt'
   print 'Current Loaded Names:'+langName+'.'
   return langName

Removed () from langName()

Here is another issue: you want to call a function l10n.buildName(), but instead you are trying to access function's local variable by doing l10n.buildName.langName. That is not possible

Since you are trying to get attribute langName from l10n.buildName function you get 'function' object has no attribute 'langName' exception

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top