5

How des one properly reject the creation of an object in a Python constructor? Consider an object:

class Triangle: def __init__(self, a, b, c): sides = [a, b, c] sides.sort() if sides[0]+sides[1] < sides[2]: return None self._a = a self._b = b self._c = c 

If the sides are not logical for a triangle, I would like to reject the creation of a Triangle object. Returning None does not prevent the creation of the Triangle object, and returning False throws an exception. What is the proper way to handle this? Should I throw some type of exception when the wrong parameters are given?

2 Answers 2

11

Either raise an exception

class Triangle: def __init__(self, a, b, c): sides = [a, b, c] sides.sort() if sides[0]+sides[1] < sides[2]: raise ValueError('invalid triangle!') self._a = a self._b = b self._c = c 

or use an assert (which raises an exception itself)

class Triangle: def __init__(self, a, b, c): sides = [a, b, c] sides.sort() assert sides[0]+sides[1] >= sides[2] self._a = a self._b = b self._c = c 

Which one is more appropriate depends on if throwing on invalid values is supposed to be part of your API (first version), or only to help find programmer errors (second version, as asserts will be skipped if you pass the -O "optimized" flag to the python interpreter).

Sign up to request clarification or add additional context in comments.

2 Comments

I'd also add a message to the ValueError, e.g., ValueError( "Invalid triangle" )
@Arthur a bit late, but I added a message as per your recommendation
2

Returning a value (even None) from a constructor is not allowed

As you suggested, should raise an exception.

class Triangle: def __init__(self, a, b, c): sides = [a, b, c] sides.sort() if sides[0]+sides[1] < sides[2]: raise ValueError() self._a = a self._b = b self._c = c 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.