By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,857 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

error in an example of oop

P: 12
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.

Expand|Select|Wrap|Line Numbers
  1. class person:
  2.     def __init__(self,name,job=None,pay=0):
  3.         self.name = name
  4.         self.job = job
  5.         self.pay = pay
  6.     if __name__ == '__main__':
  7.         bob = person('bob smith')
  8.         print (bob.name, bob.pay)
  9.  
this is error message :
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
class person:
File "<pyshell#10>", line 7, in person
bob = person('bob smith')
NameError: name 'person' is not defined

sincerely
Feb 27 '11 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 624
Your indentation is off.
Expand|Select|Wrap|Line Numbers
  1. class person:
  2.     def __init__(self,name,job=None,pay=0):
  3.         self.name = name
  4.         self.job = job
  5.         self.pay = pay
  6. ##    if __name__ == '__main__':
  7. if __name__ == '__main__':
  8.         bob = person('bob smith')
  9.         print (bob.name, bob.pay) 
Feb 27 '11 #2

P: 12
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
Feb 27 '11 #3

Expert 100+
P: 624
Yes it does. You must be doing something else that we don't know about.
Feb 27 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.