different behaviour for user defined exception with attribute args

2009-09-29 Thread Visco Shaun
Hi all

For an exception defined as below

class OptionError(Exception):
def __init__(self, args):
self.args = args
def __str__(self):
return repr(self.v)

an iteration is happening when the exception is raised

Meanwhile for almost the same structured exception replacing the
attribute 'args' with say 'value' it is not a probs.

class OptionError(Exception):
def __init__(self, args):
self.value = args
def __str__(self):
return repr(self.value)

This was frustrating because for a st. OptionError('Error') for
exception 1 output will be

OptionError: ('E', 'r', 'r', 'o', 'r') 

Meanwhile for exception 2 output will be 

OptionError: 'Error'

which is desired..Why this behaviour?

Regards
Visco

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: different behaviour for user defined exception with attribute args

2009-09-29 Thread M.-A. Lemburg
Visco Shaun wrote:
 Hi all
 
 For an exception defined as below
 
 class OptionError(Exception):
 def __init__(self, args):
 self.args = args

This should read:

def __init__(self, *args):
self.args = args

(notice the * !)

self.args holds the constructor argument tuple or list, not
a single value.

 def __str__(self):
 return repr(self.v)
 
 an iteration is happening when the exception is raised
 
 Meanwhile for almost the same structured exception replacing the
 attribute 'args' with say 'value' it is not a probs.
 
 class OptionError(Exception):
 def __init__(self, args):
 self.value = args
 def __str__(self):
 return repr(self.value)
 
 This was frustrating because for a st. OptionError('Error') for
 exception 1 output will be
 
 OptionError: ('E', 'r', 'r', 'o', 'r') 
 
 Meanwhile for exception 2 output will be 
 
 OptionError: 'Error'
 
 which is desired..Why this behaviour?

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Sep 29 2009)
 Python/Zope Consulting and Support ...http://www.egenix.com/
 mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
 mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: different behaviour for user defined exception with attribute args

2009-09-29 Thread Dave Angel

Visco Shaun wrote:

Hi all

For an exception defined as below

class OptionError(Exception):
def __init__(self, args):
self.args = args
def __str__(self):
return repr(self.v)

an iteration is happening when the exception is raised

snip

  
What is self.v intended to produce?   Perhaps you meant self.args ??  
That doesn't explain your error though.  I suspect you didn't quote your 
code accurately.


The other problem is that args is intended to be a tuple.  So you want 
to use *args for your formal parameter.


DaveA


--
http://mail.python.org/mailman/listinfo/python-list