Python OOP newbie question: C-Struct-esque constructions

Jonathan S python_hacker at example.com
Mon Aug 12 12:39:58 EDT 2002


On Sun, 11 Aug 2002 20:47:26 -0400, Jay O'Connor wrote:

> In article <20020812.073516.2103318776.1420 at cybermesa.com>, "Jay
> O'Connor" <joconnor at cybermesa.com> wrote:
> 
>> In article <pan.2002.08.12.03.42.26.693124.11376 at example.com>,
>> "Jonathan S" <python_hacker at example.com> wrote:
>> 

<--snip!-->

>> 
>>> Any suggestions as to how to do this more python-esque?
>> 
>> 
>> I'm not sure if it's moer 'python-esque' but you can take advantage of
>> the fact that object instance variables are held in an internal
>> dictionary indexed by name and build a mapping from array elements to
>> instance variables.  Here's a sample
>> 
>> #!/usr/bin/python
>> 
>> 
>> class xover_data:
>> 
>> 	# Class variable of instance variable names, order is important
>> 	VariableNames = ["frog", "s_frog", "spam"]
>> 	
>> 	def __init__ (self, aList):
>> 	
>> 		# use the class variable information to populate my instance
>> 		#variables from the given list
>> 
>> 		for i in range (0, len(xover_data.VariableNames)):
>> 			self.__dict__[xover_data.VariableNames[i]] = aList[i]
>> 			
>> 			
>> 			
>> # -- Main --
>> 
>> testList = ["ribbit", "s_ribbit", "spam"]
>> 
>> data = xover_data (testList)
>> 
>> print data.frog
> 
WOW! that's a neat trick! Tell me, are Class variables like the java 'static
final' types? i.e. constants that will be included in every instance? what's
the difference between Class Variables and instance variables?


> A slightly more sophistaced version is just to carry the list and allow
> your accessor methods to indirect through a mapping dictionary to get
> values from the list
> #!/usr/bin/python
> 
> class xover_data:
> 
> 	VariableNameMapping = {"frog":0, "s_frog":1,"spam":2}
> 	
> 	def __init__ (self, aList):
> 		self._variableArray = aList
> 		
> 	def _getVariableNamed (self, variableName):
> 			if xover_data.VariableNameMapping.has_key(variableName):
> 				return
> 				self._variableArray[xover_data.VariableNameMapping[variableName]]
> 			else:
> 				#Should probably raise a NameError here, instead return None
> 	
> 	def frog(self):
> 		return self._getVariableNamed ("frog")
> 		
> 		
> # Main
> 
> testList = ["ribbit", "s_ribbit", "spam"]
> 
> data = xover_data (testList)
> 
> print data.frog()
> 
> 
> From there, the next step is just to overwrite __getattr__ to do the
> loopkup
> 
> #!/usr/bin/python
> 
> class xover_data:
> 
> 	VariableNameMapping = {"frog":0, "s_frog":1,"spam":2}
> 	
> 	def __init__ (self, aList):
> 		self._variableArray = aList
> 		
> 		
> 	def __getattr__ (self, variableName):
> 
> 			if xover_data.VariableNameMapping.has_key(variableName):
> 				return
> 				self.__dict__["_variableArray"][xover_data.VariableNameMapping[variableName]]
> 			else:
> 				#Should probably raise a NameError here, instead return None
> 		
> # Main
> 
> testList = ["ribbit", "s_ribbit", "spam"]
> 
> data = xover_data (testList)
> 
> print data.frog
> 

wow. this is a bit elaborate for what I'm doing (at least right now) but I'm
gonna keep this in mind for something that will deserve it.

Thanks a lot!
-jonathan



More information about the Python-list mailing list