TonyW
TonyW

Reputation: 18895

Python: is entry point of execution (__main__)?

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

Upvotes: 0

Views: 863

Answers (3)

Xavier Combelle
Xavier Combelle

Reputation: 11235

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!

Upvotes: 4

Sukrit Kalra
Sukrit Kalra

Reputation: 34533

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.

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1123900

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

Upvotes: 4

Related Questions