[Tutor] __doc__ strings for attributes?
wesbrooks at gmail.com
Fri Nov 9 12:02:56 CET 2007
How can I add information to an object to explain what it expects as
it's attributes? For instance I may have an object that creates a CAD
file based on a set of default values which are set by the __init__
but can be altered before it runs. for example;
self.boundsOptions = ['cuboid', 'cylinder', 'cad']
self.boundsInfo = ['cuboid', [0.,10.,0.,10.,0.,10.]']
assert self.boundsInfo in self.boundsOptions,
"Option not recognised: %s", self.boundsInfo
if self.boundsInfo == 'cuboid'
bounds = self.boundsInfo
.... # code to make box
elif self.boundsInfo == 'cylinder'
[height, radius, noSides] = self.boundsInfo
.... # code to make cylinder
elif self.boundsInfo == 'cad'
fileName = self.boundsInfo
.... # code to load CAD file
if __name__ == '__main__':
shapeFactory0 = MakeBasicShape()
shape0 = shapeFactory.MakeIt() # a box
shapeFactory1 = MakeBasicShape()
shapeFactory1.boundsInfo = ['cylinder' ,[20.,10.,36]]
shape1 = shapeFactory.MakeIt() # a cylinder
shapeFactory2 = MakeBasicShape()
shapeFactory2.boundsInfo = ['cad' ,'/home/Wes/BigHand.stl']
shape2 = shapeFactory.MakeIt() # a CAD file
While this example could be coded with different functions for making
a box, cylinder, and loading the CAD file I wanted to use attributes
to control the object to simplify interaction with it from the user
interface code. I would like to move away from getters and setters as
they're taking up vast chunks of my code at the moment and do very
Can I also stop new attributes being added to the MakeBasicShape class?
Thanks in advance of any help.
More information about the Tutor