Reputation: 890
For example take a simple class representing a person. The only class attribute is a string representing the persons name. I want to make sure that nobody tries to pass the constructor some other type of object like an int, or list...etc. This was my first attempt below I thought that this would return an obj of type None if the argument was not a str but it still seems to return a Person obj. I come from a "more" strongly typed language background and I am a little confused as how to handle my self in python. What does pythonic style say about this situation and type safety more generally? Should I raise an exception? Or find a way to return None? Or something else entirely.
class Person:
name = None
def __init__(self, name):
if not isinstance(name, str):
return None
self.name = name
Upvotes: 1
Views: 785
Reputation: 310097
You'd want to raise an error within the __init__
method:
if not isinstance(name,basestring):
raise TypeError("I don't think that is a name ...")
*Note that basestring
also includes unicode
for python2.x, but isn't available in python3.x.
Careful though, there is nothing here to prevent a user from re-setting the person's name to a list after the person
has been constructed.
jack = Person("Jack")
jack.name = ["cheese","steak"] #???
If you want to have this safety built in, you'll need to start learning about property
.
Upvotes: 6
Reputation: 3985
I would not return None
here but rather raise and exception to signify the incorrect object type.
Upvotes: 0
Reputation: 142216
That's about it - short of using some form of decorator, but generally, type checking isn't very Pythonic, although valid in some cases, your code should be:
def __init__(self, name):
if not isinstance(name, basestring):
raise TypeError('name must be str/unicode')
# ...
Note that basestring
is the parent of str
and unicode
in Python 2.x, so this would allow both - either use str
(as you are now in 2.x) to not allow unicode (or vice versa - for any particular reason). In 3.x, basestring
doesn't exist, you've just got str
.
Upvotes: 5