How to do this? - newbie

Ronald L. Dilsavor dilsavor at erinet.com
Fri Feb 11 23:48:23 EST 2000


Thanks for all your solutions...
Here is the one I think I was after - and I got it by providing more
background on what I was after. Sorry I am not thinking totally OO yet or
even asking the question correctly.

> Regarding how I came up with such a question...
> I have a file that contains stuff like:
> sensorType = HSI
> numSensorBands = 5
> etc
>
> I am reading that file with readline and splitting the line into a list of 2
> items such as a=('sensorType', 'HSI')
>
> I then want to make sensorType an attribute of a sensor object so I can
> access it like
>
>>>> print sensor.sensorType
> 'HSI'
>
> which I think has really nice readability.
> If I go the dictionary approach, it will read more like
>
>>>> print sensor.dictionaryName['sensorType']
> 'HSI'
>
> which in my opinion is a little less pleasing to the eye.

Paul Foley wrote:
Ah, I see.  That makes a difference!  There's no need to create new
local variables.  Conceptually, use a dict enclosed in a class, with
methods that hide accesses so that you can use sensor.sensorType() [or
sensor.sensorType, if you prefer] rather than
sensor.dictionary['sensorType'].  But note that the way class
attributes work in Python is that there is _already_ a dict, called
__dict__, in each instance, containing the attributes.  So
sensor.sensorType is equivalent to sensor.__dict__['sensorType'], and
you can just go ahead and use __dict__ to store your mappings.




More information about the Python-list mailing list