error in an example of oop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PyGIS
    New Member
    • Feb 2011
    • 12

    error in an example of oop

    Hi:
    I'm beginner in python scripting . i begin with learning python book .I write code below . this is an example of this book for oop programming .but i encounter to an error . I don't know what's my fault in this code . please help me.

    Code:
    class person:
    	def __init__(self,name,job=None,pay=0):
    		self.name = name
    		self.job = job
    		self.pay = pay
    	if __name__ == '__main__':
    		bob = person('bob smith')
    		print (bob.name, bob.pay)
    this is error message :
    Traceback (most recent call last):
    File "<pyshell#1 0>", line 1, in <module>
    class person:
    File "<pyshell#1 0>", line 7, in person
    bob = person('bob smith')
    NameError: name 'person' is not defined

    sincerely
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Your indentation is off.
    Code:
    class person:
        def __init__(self,name,job=None,pay=0):
            self.name = name
            self.job = job
            self.pay = pay
    ##    if __name__ == '__main__':
    if __name__ == '__main__':
            bob = person('bob smith')
            print (bob.name, bob.pay)

    Comment

    • PyGIS
      New Member
      • Feb 2011
      • 12

      #3
      Hi:
      thnx for your help .it was work. i had an another fault:i think that class name must be begin with an uppercase letter .
      sincerely

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        Yes it does. You must be doing something else that we don't know about.

        Comment

        Working...