1

I am using flask. When I do not pass StartingSequenceNumber to flask app then How can I handle null object.

  class Meta():


    def __init__(self, j):
        self.__dict__ = json.loads(j)

in bootstrap.py

   meta = Meta(request.get_data().decode())
   if meta.StartingSequenceNumber is not None:
     # do something

Error : AttributeError: 'Meta' object has no attribute 'StartingSequenceNumber'

lucy
  • 4,136
  • 5
  • 30
  • 47

1 Answers1

0

You could use the hasattr() built-in function (https://docs.python.org/3/library/functions.html#hasattr) which will return True if the object has the attribute :

if hasattr(object, 'attribute'): 
    # do smthg
else:
    # do smthg else

But it would be better to used try & except blocks and throw an AttributeError

try:
    doSmthg()
except AttributeError:
    # do smthg else
Gwendal Grelier
  • 526
  • 1
  • 7
  • 21