-1

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.

3 Answers3

1

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('#')]
mhlester
  • 22,781
  • 10
  • 52
  • 75
  • Fixed but now it gives this: ''TypeError: unbound method buildName() must be called with l10n instance as first argument (got nothing instead)'' – user3152708 Feb 04 '14 at 06:14
  • There's not enough information to know for sure what the cause is of that. Make sure l10n is an *instance* of your class, not the class itself. If that doesn't do the trick, you should accept an answer here and post that as a new question – mhlester Feb 04 '14 at 06:17
0

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()

James Sapam
  • 16,036
  • 12
  • 50
  • 73
0

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

Yevgen Yampolskiy
  • 7,022
  • 3
  • 26
  • 23