HELP: Weird attribute behavior...

Steven D. Majewski sdm7g at Virginia.EDU
Fri Jun 1 15:53:54 EDT 2001


On Fri, 1 Jun 2001, Ralph Allan Rice wrote:

> I am developing a class that stores device reading information.  It
> looks like this:
> 
> 
> import string
> import dbi, odbc
> 
> 
> class Reading:
> 	__data  =  { }
> 	
> 	def __init__(self, type, id, timezone, datestamp, value):
> 		self.__data['id'] = id
> 		self.__data['timezone'] = timezone
> 		self.__data['datestamp'] =datestamp
> 		self.__data['value'] = value
> 		self.__data['type']= type
> 		
> 		


change that to:

 class Reading:
						## move __data 
 	def __init__(self, type, id, timezone, datestamp, value):
	 	self.__data  =  { }		## to here 
 		self.__data['id'] = id
 		self.__data['timezone'] = timezone
 		self.__data['datestamp'] =datestamp
 		self.__data['value'] = value
 		self.__data['type']= type
 		

In your example, __data is a shared class attribute. 
If you create it in the __init__ method, then it's owned by
that instance only. 

-- Steve Majewski






More information about the Python-list mailing list