2

How to get rid of this NameError?

main.py

from com.domain.model import Employee

e1 = Employee("")

model.py

class Person(object):
    def __init__(self,fname):
        setfname(fname)
    def setfname(fname): self._fname = fname

class Employee(Person):
    def __init__(self,fname): super(Employee,self).__init__(fname)

NameError: global name 'setfname' is not defined


main.py

com/

--domain/

----model.py

Patt Mehta
  • 4,110
  • 1
  • 23
  • 47

1 Answers1

3

Qualify setfname as self.setfname. In addition to that, the instance method setfname should have self as the first parameter.

class Person(object):
    def __init__(self, fname):
        self.setfname(fname)
    #   ^^^^^
    def setfname(self, fname):
    #            ^^^^^^
        self._fname = fname
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Is it posb'l to create functions that are for personal use of a class? – Patt Mehta Sep 01 '13 at 16:56
  • @GLES, I don't understand the **personal use of a class**? Do you mean private method? – falsetru Sep 01 '13 at 17:04
  • @GLES, See [Does python have 'private' variables in classes](http://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes) if you mean private method. – falsetru Sep 01 '13 at 17:06