Skip to main content
Engineering LibreTexts

6.2: Attributes

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    You can assign values to an instance using dot notation:

    >>> blank.x = 3.0
    >>> blank.y = 4.0

    This syntax is similar to the syntax for selecting a variable from a module, such as math.pi or string.whitespace. In this case, though, we are assigning values to named elements of an object. These elements are called attributes.

    As a noun, “AT-trib-ute” is pronounced with emphasis on the first syllable, as opposed to “a-TRIB-ute,” which is a verb.

    The following diagram shows the result of these assignments. A state diagram that shows an object and its attributes is called an object diagram; see Figure 15.2.1.

    Object diagram.
    Figure \(\PageIndex{1}\): Object diagram.

    The variable blank refers to a Point object, which contains two attributes. Each attribute refers to a floating-point number.

    You can read the value of an attribute using the same syntax:

    >>> print blank.y
    >>> x = blank.x
    >>> print x

    The expression blank.x means, “Go to the object blank refers to and get the value of x.” In this case, we assign that value to a variable named x. There is no conflict between the variable x and the attribute x.

    You can use dot notation as part of any expression. For example:

    >>> print '(%g, %g)' % (blank.x, blank.y)
    (3.0, 4.0)
    >>> distance = math.sqrt(blank.x**2 + blank.y**2)
    >>> print distance

    You can pass an instance as an argument in the usual way. For example:

    def print_point(p):
        print '(%g, %g)' % (p.x, p.y)

    print_point takes a point as an argument and displays it in mathematical notation. To invoke it, you can pass blank as an argument:

    >>> print_point(blank)
    (3.0, 4.0)

    Inside the function, p is an alias for blank, so if the function modifies p, blank changes.

    Exercise \(\PageIndex{1}\)

    Write a function called distance_between_points that takes two Points as arguments and returns the distance between them.

    This page titled 6.2: Attributes is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .

    • Was this article helpful?