0

I wrote the following python code and tried to use __main__ to initialize two class objects, but I got the following errors when running python classPerson.py. I wonder what's the correct way of initializing class objects in python.

Person(object):
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary


    def getName():
        return self.name

    def getSalary():
        return self.salary

    if __name__ == '__main__':
        bob = Person('Bob Smith', 40000)

        sue = Person('Sue Funk', 35000)

        print 'bob makes a salary of %d' % (bob.getSalary)
        print 'sue's full name is %s' % (sue.getName)

Errors:

Traceback (most recent call last):
  File "classTest1.py", line 1, in <module>
    class Person(object):
  File "classTest1.py", line 14, in Person
    bob = Person('Bob Smith', 40000)
NameError: name 'Person' is not defined
1
  • Your code is invalid, as posted, because the last string literal won't compile (single quotes around a value with an un-escaped single quote in it).
    – Martijn Pieters
    Jan 27, 2014 at 18:17

3 Answers 3

4

You need to unindent the if __name__ block to not be part of the Person class.

It is being executed too early and the Person class is still being built at that time.

By indenting it to the same level as the methods in the Person class body, you made the test part of the class definition. It is being executed as when the class is being defined still, so the Person class reference does not yet exist.

Corrected code:

class Person(object):
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def getName(self):
        return self.name

    def getSalary(self):
        return self.salary

if __name__ == '__main__':
    bob = Person('Bob Smith', 40000)

    sue = Person('Sue Funk', 35000)

    print 'bob makes a salary of %d' % (bob.getSalary())
    print "sue's full name is %s" % (sue.getName())

Note:

  1. you also need to call the getSalary() and getName() methods.
  2. Give all methods a self parameter.
  3. use correct quoting for your strings (sue's contains a single quote, best to use double quotes for delimiting that string).
3
  • And according to PEP-8 the methods should be named get_name and get_salary.
    – Matthias
    Jan 27, 2014 at 18:19
  • 1
    @Matthias: I'm not fussy about naming conventions for newbies. That all can come later.
    – Martijn Pieters
    Jan 27, 2014 at 18:21
  • That's why I added it as a comment and didn't try to write an own answer.
    – Matthias
    Jan 27, 2014 at 18:23
4

you can't use the Person class while it is defined you have to put it one indentation level down

class Person(object):
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary


    def getName(self):
        return self.name

    def getSalary(self):
        return self.salary

if __name__ == '__main__':
    bob = Person('Bob Smith', 40000)

    sue = Person('Sue Funk', 35000)

    print 'bob makes a salary of %d' % (bob.getSalary())
    print "sue's full name is %s" % (sue.getName())

EDIT corrected last statement according Sukrit Kalra comment

EDIT the correct way to call a method is using parenthesis and the correct way to define it is using self in parameters but the idiomatic way to use the class is to not use getters and setters so

class Person(object):
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary


if __name__ == '__main__':
    bob = Person('Bob Smith', 40000)

    sue = Person('Sue Funk', 35000)

    print 'bob makes a salary of %d' % (bob.salary)
    print "sue's full name is %s" % (sue.name)

python is not java!

3
  • Check the last print statement. :) Jan 27, 2014 at 18:18
  • There are more problems still; you need to pay attention to how those methods are going to work (hint: calling them would be a start, and giving the methods the correct parameters would also help).
    – Martijn Pieters
    Jan 27, 2014 at 18:25
  • corrected it, did not care how few not idiomatic python code it was Jan 27, 2014 at 19:43
2

You have indented the if __name__ == "__main__" block one level too far. Try running

class Person(object):
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary


    def getName(self):
        return self.name

    def getSalary(self):
        return self.salary

if __name__ == '__main__':
    bob = Person('Bob Smith', 40000)
    sue = Person('Sue Funk', 35000)
    print 'bob makes a salary of %d' % (bob.getSalary())
    print "sue's full name is %s" % (sue.getName())

Since, in your code, the if block is in the Person class and you are making objects of that class before the class is built, it is giving you an error.

3
  • wow, you guys are fast! Thank you very much! You guys reminded me the importance of indentation in python! :) appreciate it.
    – TonyW
    Jan 27, 2014 at 18:19
  • There are several more problems with the code still; see my answer for addressing one more problem Sukrit has not yet addressed. :-)
    – Martijn Pieters
    Jan 27, 2014 at 18:24
  • Can't believe I missed that Martijn! Will fix it in the code right away. Thanks. :) Jan 27, 2014 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.