Skip to main content
Engineering LibreTexts

6.7: Debugging

  • Page ID
  • When you start working with objects, you are likely to encounter some new exceptions. If you try to access an attribute that doesn’t exist, you get an AttributeError:

    >>> p = Point()
    >>> print p.z
    AttributeError: Point instance has no attribute 'z'

    If you are not sure what type an object is, you can ask:

    >>> type(p)
    <type '__main__.Point'>

    If you are not sure whether an object has a particular attribute, you can use the built-in function hasattr:

    >>> hasattr(p, 'x')
    >>> hasattr(p, 'z')

    The first argument can be any object; the second argument is a string that contains the name of the attribute.

    • Was this article helpful?