By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 935 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

Checking parameters prior to object initialisation

P: n/a
Fairly new to Python (and loving it!)

In C++ (gack!) I got used to creating a helper function with each class to
check the class object initialisation parameters prior to creating the
object.

In Python, this would be
-----------------------------------------------
import example

if example.ParametersOK(a, b, c, d):
newObj = example.Example(a, b, c, d)
else:
print "Error in parameters"
-----------------------------------------------

I presume this would still be considered good practise in Python, or is
there some other, preferred, method?
Cheers,
Brett McSweeney

May 24 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Brett_McS wrote:
In C++ (gack!) I got used to creating a helper function with each class to
check the class object initialisation parameters prior to creating the
object.

In Python, this would be
-----------------------------------------------
import example

if example.ParametersOK(a, b, c, d):
newObj = example.Example(a, b, c, d)
else:
print "Error in parameters"
-----------------------------------------------

I presume this would still be considered good practise in Python, or is
there some other, preferred, method?
Use exceptions to signal wrong parameters and move the parametersOk() test
into the initializer

class Example:
def __init__(self, a, b, c, d):
if a < 0:
raise ValueError("Negative length not allowed")
#...

Write a factory if
- creating the Example instance carries too much overhead and wrong
parameters are likely, or
- the checks are costly and you often get parameters known to be OK.

def make_example(a, b, c, d):
if a < 0:
raise ValueError("Negative length not allowed")
#...
return Example(a, b, c, d)

Example object creation then becomes

try:
newObj = example.Example(1,2,3,4) # or make_example(...)
except ValueError, e:
print e # you will get a specific message here

If the checks still have to occur in multiple places in your code you are of
course free to factor them out in a separate function.

Peter

May 24 '07 #2

P: n/a
Brett_McS a écrit :
Fairly new to Python (and loving it!)

In C++ (gack!) I got used to creating a helper function with each class to
check the class object initialisation parameters prior to creating the
object.

In Python, this would be
-----------------------------------------------
import example

if example.ParametersOK(a, b, c, d):
newObj = example.Example(a, b, c, d)
else:
print "Error in parameters"
-----------------------------------------------

I presume this would still be considered good practise in Python,
I don't think so.
or is
there some other, preferred, method?
If you really have to check, do it in the object's initializer (the
__init__ method), and raise an exception if necessary.

May 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.