473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

python class problem

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
3 2020
bvdet
2,851 Expert Mod 2GB
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
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
dwblas
626 Expert 512MB
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

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

Similar topics

3
by: Shi Sherebrin | last post by:
A colleague wants some functionality that I've built in a Python class available to 'drop' into her Visual C++ project. Some time ago she gave up on trying to use 'regular' COM servers, since...
2
by: JM | last post by:
Hello everyone, Does anybody know about, have documentation on, or have any code samples on how to access class members from a python class in C++. Say I have a simple python script: ...
1
by: Devrim Erdem | last post by:
Hello, My application has a c++ plugin interface. To create a plugin, a class is derived from the Plugin class and compiled into a shared object ( so or dll ). I would like to allow...
18
by: Tad Marko | last post by:
Howdy! I'm trying to get my head around Python and I've come to realize that there are a lot of idioms in Python that are a bit different than in other languages. I have a class similar to what...
1
by: Stephane Ninin | last post by:
Hello all, I am trying to play with nested class in a script I am making, and I am not sure I really understand how they work. Here is some code: __all__ =
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
0
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com...
0
by: abarun22 | last post by:
Hi I am facing a problem while including a C header file in the SWIG interface file. However the problem does not occur when i directly copy the contents of header file in the same place. My...
17
by: Sunburned Surveyor | last post by:
I was thinking of a way I could make writing Python Class Files a little less painful. I was considering a Ptyhon script that read a file with a list of property names and method names and then...
0
by: Mark Lutz | last post by:
A page describing our 2009 Python class offerings has just been posted here: http://home.earthlink.net/~python-training/2009-public-classes.htm The first class in 2009 will be held January...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.