3

When I try to run the code of a class I'm writing, I get an AttributeError and I'm not sure why. The specific error is as follows:

    self.marker = self.markers[marker[1:]]
AttributeError: 'TTYFigureData' object has no attribute 'markers'

Here is part of the class I'm writing:

class TTYFigureData(object):
    """
    data container of TTYFigure
    """
    def __init__(
        self,
        x,                      # x values
        y,                      # y values
        marker          = "_.", # datum marker
        plot_slope      = True
        ):
        self.x          = x
        self.y          = y
        self.plot_slope = plot_slope
        self.set_marker(marker)
        self.markers = {
            "-" : u"None" ,
            "," : u"\u2219"
        }

    def set_marker(
        self,
        marker
        ):
        if marker in [None, "None", u"None", ""]:
            self.plot_slope = True
            self.marker = ""
        elif marker[0] == "_":
            self.marker = self.markers[marker[1:]]
        else:
            self.marker = marker

Where am I going wrong?

6
  • 3
    Your code style is somewhat... idiosyncratic ("awful" seemed a bit pejorative), which makes it hard to follow. Note, though, that you call self.set_marker before initialising self.markers. Given that self.markers appears to be fixed, why not make it a class attribute? Commented Sep 28, 2015 at 13:12
  • 2
    Please do post a full traceback for Python errors. For this case, for example, the fact that set_marker() is called from __init__ is a crucial clue as to what is going wrong, the traceback would show that. Commented Sep 28, 2015 at 13:15
  • @jonsharpe Ah, that's it! Well spotted. Thanks for looking. The code is rough and I know the approach of having a new line for each function argument is not considered standard. Is the approach really so bad, particularly for large numbers of arguments? Commented Sep 28, 2015 at 13:15
  • @MartijnPieters Thanks for that tip. You're quite right. Commented Sep 28, 2015 at 13:16
  • @d3pd: yes, it made it very hard to distinguish between argument list and method body, for example. Please do read the Python styleguide, which offers better alternatives. Commented Sep 28, 2015 at 13:16

2 Answers 2

6

In your __init__ method, you call self.set_marker() before you set self.markers:

self.set_marker(marker)
self.markers = {
    "-" : u"None" ,
    "," : u"\u2219"
}

So when set_marker() runs, there is no self.markers yet. Move the call down a line:

self.markers = {
    "-" : u"None" ,
    "," : u"\u2219"
}
self.set_marker(marker)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your clear solution. It was a silly thing to miss.
3

Martijn's answer explains the problem and gives the minimal solution. However, given that self.markers appears to be constant, I would make it a class attribute rather than recreating it for every instance:

class TTYFigureData(object):
    """Data container of TTYFigure."""

    MARKERS = {
        "-": u"None" ,
        ",": u"\u2219",
    }

    def __init__(self, x, y, marker='_.', plot_slope=True):
        """Document parameters here as required."""
        self.x = x
        self.y = y
        self.plot_slope = plot_slope
        self.set_marker(marker)

    def set_marker(self, marker):
        """See also here - usage guidance is also good."""
        if marker in [None, "None", u"None", ""]:
            self.plot_slope = True
            self.marker = ""
        elif marker[0] == "_":
            self.marker = self.MARKERS[marker[1:]]
        else:
            self.marker = marker

(note also changes to style in line with the official guidance)

1 Comment

Thank you for your clear solution and for your guidance on changing the markers attribute from an instance attribute to a class attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.