[Solved] python self is acting up


Normally, when you create an instance of a class draw, the initialization routine draw.__init__ is automatically called for you, and the self argument is added implicitly, so you don’t specify it explicitly. Here’s the normal case:

d = draw( canvas, 20, 40, ... )

…and indeed that’s just what you seem to have assumed. Your mistake is calling the constructor draw.__init__ explicitly. There are some situations when you might want to do that, for example while initializing an instance of a subclass. But these tend to be more complicated/specialized cases. If you do it, then you need to supply the self explicitly in your call, and you did not do this. So what happened is that your canvas value got assigned to self, 20 got assigned to canvas (hence the error), and so on.

d = draw( canvas, 20, 40, ... ) # you mean this
draw.__init__( d, canvas, 20, 40, ... )  # not this (which is correct if you're calling the draw initializer on some pre-existing object d)

I also notice that you’ve defined a method axies that wants to use the value of canvas. As it’s written, that code will be looking for a global variable called canvas. A much safer way to design things is to make that variable an attribute of your draw instance. That means assigning self.canvas = canvas in the body of __init__ and then referring to it as self.canvas rather than just canvas in other methods (forgetting that last part is a really common mistake for me).

solved python self is acting up