Forum Archive

Unable to call base class draw method

bitbucket158

In Pythonista I created a new script using the "Scene with Layers" template. I then attempted to define my own custom layer class using the below.

class MyLayer (Layer):
  def draw(self):
    super(MyLayer,self).draw()

My hope was to allow the base class to draw then add some custom rendering on top .

I changed the code in the template scene setup method so it created instances of MyLayer instead of Layer to test it.

When I run I get the following error.

"TypeError: draw() takes exactly 1 argument (2 given)"

Any idea what I'm doing wrong?

bitbucket158

ccc I don't see any text in your reply. It appears to be empty :)

ccc

I thought I had a solution but it did not work :-(

I could not spot the problem but you might try looking at the draw() methods in:

import inspect, scene
print(inspect.getsource(scene.Layer))
print(inspect.getsource(scene.Scene))
bitbucket158

Strange ; I know I've called base class methods in other contexts but never draw and never derived from any scene classes .

bitbucket158

I got it to work! The inspect code you posted helped. Looks like the Layer draw method takes a second optional arg, a (alpha). Adding that got rid of the error. Odd since it's optional though, but it worked!

Here's the working code.

class MyLayer (Layer):
  def draw(self, a=1.0):
    super(MyLayer,self).draw(a)
ccc
# The following only works with single inheritance classes like scene.Layer...
    super(self.__class__, self).draw(a)
bitbucket158

What do you mean? I'm not following your last comment.

ccc
class MyLayer (Layer):
  def draw(self, a=1.0):
#   super(MyLayer,self).draw(a)
    super(self.__class__, self).draw(a)  # Alternate syntax that does not hardcode the classname
bitbucket158

Ah ok! Yes that is nicer. Thanks!

bitbucket158

I ended up creating a function to make this even cleaner.

def _super(self):
  return super(self.__class__,self)

With this it makes it much cleaner when you want to invoke the base class.

class MyLayer (Layer):
  def draw(self,a=1.0):
    _super(self).draw(a)

A lot nicer if you're coding on an iPhone with the small display :)