How to do this? - newbie

Thomas A. Bryan tbryan at python.net
Sat Feb 12 07:33:54 EST 2000


"Ronald L. Dilsavor" wrote:
> 
> 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.

Don't worry about the OO thing, but it is in general easier for others to 
help if you give a complete description of what you're trying to do.

I have a few more suggestions below...
 
> > 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'

That's reasonable.

> > 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.

Note that by subclassing UserDict.UserDict, you can have a class 
that acts like a dictiory.  That is, you can do something like this

import UserDict
import string
class Sensor(UserDict.UserDict):
    # your own specialization here
    # see UserDict.py in the Python library on your system
    # I'll just pass for now and let UserDict handle all of the behavior.
    pass

sensor = Sensor()  # create a new Sensor
sensor['sensorType'] = 'HSI'  # treat the class like a dictionary
line = 'numSensorBands = 5'  # more like what you're doing
(key,value) = string.split(line,'=')
sensor[string.strip(key)] = string.strip(value)
print sensor
print sensor['sensorType']
print sensor['numSensorBands']

> 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.

I agree, except that the "wrap a dict in a class" implies UserDict to me.
If you really prefer writing 

print sensor.sensorType 
to writing 
print sensor['sensorType']

then you could use setattr to set the class attributes in the first place

class Sensor:
    def __init__(self):
        self.sensorType = None  # force the existence of various attributes

sensor = Sensor()
setattr(sensor, 'sensorType', 'HSI')  # sensor now has a sensorType attribute
line = 'numSensorBands = 5'  # more like what you're doing
(key,value) = string.split(line,'=')
setattr(sensor, string.strip(key), string.strip(value))
print sensor
print sensor.sensorType
print sensor.numSensorBands
# note that numSensorBands wasn't defined in the class constructor, 
# we added it at run time by parsing the file

If you're using a lot of getattr and setattr everywhere in your 
code, then you might prefer using the UserDict approach because 
you rarely get to write sensor.sensorType.  (If you don't need to 
specialize the behavior of UserDict, then you really could use 
a regular Python dictionary because it will be faster with the 
same functionality.)  If you just need to use the setattr to 
create the objects from a file and can then use the sensor.sensorType 
notation everywhere else, you might prefer avoiding UserDict.  
If every file has roughly the same attributes, then I'd suggest
setting the values to some initial defaults (like the 
self.sensorType = None above) in the class constructor so that you 
never get an AttributeError when someone forgets to put one of the 
sensor's attributes in a file.

---Tom
subsequent



More information about the Python-list mailing list