Validation of parameters

Paddy paddy3118 at googlemail.com
Sun Aug 10 10:03:46 EDT 2008


On Aug 10, 9:13 am, Kless <jonas.... at googlemail.com> 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
like:

  self._foo

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):
		self._foo


>>> dummy = tmp()

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

- Paddy.



More information about the Python-list mailing list