Lets say I have the following Python code:
x = some_product()
name = x.name
first_child = x.child_list[0]
link = x.link
id = x.id
A problem might occur in line 3, when x.child_list is None. This obviously gives me a TypeError, saying that:
'NoneType' Object has no attribute '_____getitem_____'
What I want to do is, whenever x.child_list[0] gives a TypeError, simply ignore that line and go to the next line, which is "link = x.link"...
So I'm guessing something like this:
try:
x = some_product()
name = x.name
first_child = x.child_list[0]
link = x.link
id = x.id
Except TypeError:
# Pass, Ignore the statement that gives exception..
What should I put under the Except block? Or is there some other way to do this?
I know I can use If x.child_list is not None: ..., but my actual code is a lot more complicated, and I was wondering if there is a more pythonic way to do this