Steven D'Aprano wrote: > On Sat, 16 Dec 2006 03:54:52 -0800, [EMAIL PROTECTED] wrote: > > > Hi, In the following program, I have a class Test which has a property > > x. Its setx function gets a string value and converts it into a float > > and stores into it. > > [snip code] > > Python isn't Java. Are you sure you need properties?
I do not know Java. But, object.x = value looks much better than object.set_x(value) . Is there any harm in doing it, provided I have to do more than just storing the value. > > > > I am looking for a way to call func1's exception handler also. > > Basically I want to have the class's error handler as the basic text > > based one, and in the calling side, If I have gui, I will pop-up a > > window and say the error message. > > One solution is to remove the exception handling inside the class and > > leave the entire thing to the caller (software people call this > > client?) side -- if the caller has access to gui it will use gui or > > else will print the message. Any other way? > > The exception is consumed by the try...except block inside the class, > so func1 never sees the exception. It might as well not exist. > > Generally, you should keep your class as simple as possible. It shouldn't > try to manage any exception it can't recover from. In your case, the class > can't recover from a failure of float(strvalue), so it shouldn't consume > the exception. Two ways of doing that: > > def _setx(self, strvalue): > self._x = float(strvalue) # just let the exception propagate > > or > > def _setx(self, strvalue): > try: > self._x = float(strvalue) > except ValueError: > raise SomeError('could not set x attribute to %s' % strvalue) > > where SomeError should be either ValueError or possibly some custom > exception (say, MyClassException). > > In either case, the error handling is separate from the object that > generates the error. And that is as it should be. Now your class can > remain the same, no matter how the rest of your program handles the > exception. > > By the way, if you want your class to generate warnings, perhaps you > should investigate the Warnings module: > > import warnings > help(warnings) I will go through it. Thanks a lot. > > > > -- > Steven. -- http://mail.python.org/mailman/listinfo/python-list