validation - Python - How to handle bad parameters when creating new instance? -


what's preferred way handle bad parameters when creating new instance of class?

for example, in card game might have card class can take club, heart, diamond or spade suit when creating new instance (as value). how handle passing 'kitten' suit?

i'm new python, know bit of objective-c , swift, , think in languages make suit enum , compiler might pick incorrect value or return nil (ocj-c) or optional (swift) init of card class.

i understand cannot return 'nil' in python (i.e. not create instance) perhaps should return 'none' suit (or whatever member affected) or throw exception? or should validate inputs first?

the class __init__ method should raise valueerror if given incorrect. when in doubt, check out zen of python -- errors should never pass silently.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -