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

python class problem

P: 2
I have a problem w python class, I come form java and its so diferent, lets show problem

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. class Client:  #define the class
  4.  
  5.   def __init__(self,name):
  6.     self.name_client=name
  7.  
  8.  
  9. if __name__=="__main__": #starting point
  10.  
  11. coco = raw_input("Name of the client")
  12. coco = Client(coco)
  13. print coco.name_client
  14. print coco
  15.  

What python prints to me is

Name of the client

# lets say we input Jhon

Jhon
<__main__.Cliente instance at 0x01EEFF58>


What the hell its <__main__.Cliente instance at 0x01EEFF58> ?? I dont undestand why puts that value and not Jhon.

Need help w this please
May 14 '11 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Overload repr() to display whatever you want.
Expand|Select|Wrap|Line Numbers
  1. >>> class Client:
  2. ...     def __init__(self, name):
  3. ...         self.client_name = name
  4. ...     def __repr__(self):
  5. ...         return self.client_name
  6. ...     
  7. >>> name = raw_input("Enter name")
  8. >>> name
  9. 'Bill'
  10. >>> client = Client(name)
  11. >>> client
  12. Bill
  13. >>> 
May 14 '11 #2

P: 2
Well what I really want is make a class, that when I call it, makes an instance of class Client,

w the name the user inputs w the keyboard like

x=Raw_input("Enter teh name")
>> Bill
then program makes
Bill = Cliente("Bill")
and adds the Client instance name to a list, so I can remember or lsit all theclients
May 14 '11 #3

Expert 100+
P: 624
You are using the same variable for two different things, the name of the client and the class instance.
Expand|Select|Wrap|Line Numbers
  1. coco = raw_input("Name of the client")
  2. class_instance = Client(coco)
  3. print class_instance.client_name
  4. print coco 
To use a list or dictionary:
Expand|Select|Wrap|Line Numbers
  1. class Client:
  2.     def __init__(self, name):
  3.         self.client_name = name
  4.         self.a_number = 1
  5.  
  6. if __name__=="__main__": #starting point
  7.  
  8.     class_instance_list = []
  9.     for name in ["Bill", "Sue", "Tom"]:
  10.         class_instance = Client(name)
  11.         class_instance_list.append(class_instance)
  12.  
  13.     print len(class_instance_list), "clients added\n"
  14.     for class_instance in class_instance_list:
  15.         print class_instance.client_name, class_instance.a_number 
May 16 '11 #4

Post your reply

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