Validation of parameters

Paddy paddy3118 at
Sun Aug 10 16:03:46 CEST 2008

On Aug 10, 9:13 am, Kless <jonas.... at> wrote:
> Which is the best mode for validation of parameters? I'm using the
> next way but it is not well testable.
> -----------------------------
> if not self._foo:
>     try:
>         raise ValueError('ValueError: foo not found')
>     except ValueError, err:
>         print (err.message)
>         print("Valid values: %s\n" % self.valid_foo)
>         sys.exit(1)
> ...
> ...
> -----------------------------

If you just want the absense of _foo to raise an exception then why to
just reference self._foo and leave the hard work to ptyhon? Something


If it is present then fine, if it is not then an exception is raised.

Here it is in the Python shell:
>>> class tmp(object):
	def __init__(self):

>>> dummy = tmp()

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    dummy = tmp()
  File "<pyshell#3>", line 3, in __init__
AttributeError: 'tmp' object has no attribute '_foo'

- Paddy.

More information about the Python-list mailing list